1、初始化SoundPool
maxStreams : 最大的流数量
streamType : 流的类型 AudioManager.STREAM_SYSTEM
srcQuality : 音频质量 默认 0
soundPool=new SoundPool(int maxStreams , int streamType , int srcQuality)
2、加载音频 :一般把音频文件放置在res 的raw文件下
context : 上下文
resId : R.raw. beep
priority : 优先级 设置为1即可
使用加载音频
soundPool.load(Context context , int resId , int priority);
3、播放音频 :
soundId : 声音的id 就是2中load 音频的顺序
leftVolume : 左声道音量控制 0.0 -- 1.0
rightVolume : 右声道音量控制 0.0 -- 1.0
priority : 优先级 0最低
loop : 0不循环 1循环
rate : 1 正常播放
soundPool.play(int soundId , float leftVolume , float rightVolume , int priority , int loop , float rate);
4、小结
//初始化SoundPool
SoundPool soundPool = new SoundPool(10 , AudioManager.STREAM_SYSTEM, 5);
//加载deep音频文件
soundPool(this , R.raw.been);
//播放音频
soundPool.play(1,1, 1, 0, 0, 1);
5、封装 SoundPoolUtils
public class SoundPoolUtils
{
//SoundPool 对象
public static SoundPool mSoundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
public static SoundPoolUtils soundPoolUtils;
// 上下文
static Context mContext;
/**
* 初始化 SoundPool
*
* @param context 上下文
*
* @return
*/
public static SoundPoolUtils init(Context context)
{
if (soundPoolUtils == null) {
soundPoolUtils = new SoundPoolUtils();
}
// 初始化声音
mContext = context;
mSoundPool.load(mContext, R.raw.beep, 1);
mSoundPool.load(mContext, R.raw.beep2, 2);
return soundPoolUtils;
}
/**
* 播放声音
*
* @param soundId load 进去的声音编号
*/
public static void play(int soundId)
{
mSoundPool.play(soundId, 1, 1, 0, 0, 1);
}
}
转载:http://blog.csdn.net/yy1300326388/article/details/47044869