SoundPool介绍
SoundPool一般用来 播放密集,急促而又短暂的音效,常用在游戏、铃声,还可以用到信息、聊天信息提示音等。
SoundPool对象可以看作是一个可以从APK中导入资源 或者从文件系统中载入文件的样本集合。它利用MediaPlayer服务为音频解码为一个原始16位 PCM流。这个特性使得应用程序可以进行流压缩,而无须忍受在播放音频时解压所带来的CPU 负载和延时。
1.音效添加
在【res】文件夹的【raw】文件夹(没有这个文件夹的创建一个)中添加一段音效,格式为wav格式,命名为【ring】,全称为【ring.wav】
2.方法设置
public class SoundPoolUtil {
@SuppressLint("StaticFieldLeak")
private volatile static SoundPoolUtil mInstance;
private SoundPool mSoundPool;
private Context mContext;
public static int soundPlayId = 0;
private SoundPoolUtil() {
}
public static SoundPoolUtil getInstance() {
if (mInstance == null) {
synchronized (SoundPoolUtil.class) {
mInstance = new SoundPoolUtil();
}
}
return mInstance;
}
/**
* 需要先初始化再调用playSound
*/
public void init(Context context) {
mContext = context;
if (Build.VERSION.SDK_INT >= 21) {
SoundPool.Builder builder = new SoundPool.Builder();
// 传入最多播放音频数量,
builder.setMaxStreams(1);
// AudioAttributes是一个封装音频各种属性的方法
AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
// 设置音频流的合适的属性
attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
// 加载一个AudioAttributes
builder.setAudioAttributes(attrBuilder.build());
mSoundPool = builder.build();
} else {
/**
* 第一个参数:SoundPool对象的最大并发流数
* 第二个参数:AudioManager中描述的音频流类型
* 第三个参数:采样率转换器的质量。 目前没有效果。 使用0作为默认值。
*/
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
}
}
/**
* 播放音频
*/
public void playSound() {
//加载音频,获取音频load的ID
int resSoundId = mSoundPool.load(mContext, R.raw.ring, 1);
// 异步需要等待加载完成,音频才能播放成功
mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
if (status == 0) {
Log.e("MainAvtivity", "开始播放铃声");
soundPlayId = mSoundPool.play(resSoundId,
1, // 左声道音量取值范围0-1
1, // 右声道音量取值范围0-1
1, // 优先级
-1, // 循环播放次数 设为-1代表循环播放
1); // 回放速度,该值在0.5-2.0之间 1为正常速度
}
}
});
// 当loop值为“0”时,播放模式为单次模式;当loop值为一个正整数时,loop值意味着可以播放的次数。
// mSoundPool.setLoop(soundPlayId, -1);//这个也可以设置无限循环
}
/**
* 停止播放声音
*/
public void stopSound() {
if (mSoundPool != null) {
if (soundPlayId != 0) {
mSoundPool.stop(soundPlayId);
soundPlayId = 0;
}
if (soundPlayId != 0) {
mSoundPool.unload(soundPlayId);
onDestroy();
soundPlayId = 0;
}
}
}
/**
* 释放资源
*/
public void onDestroy() {
if (mSoundPool != null) {
mSoundPool.autoPause();
mSoundPool.unload(R.raw.ring);
mSoundPool.release();
mSoundPool = null;
}
}
}
3.调用
//首先需要初始化
SoundPoolUtil.getInstance().init(this);
//在需要时调用播放
SoundPoolUtil.getInstance().playSound();
//在需要时调用停止
SoundPoolUtil.getInstance().stopSound();
//在不需要时进行销毁
SoundPoolUtil.getInstance().onDestroy();