SoundPool为音频池,适合在应用程序中的播放按键音或消息提示音等,也适合在游戏中实现密集而短暂的声,例如,多个飞机的爆炸声等。
使用SoundPool分为三个步骤:
1.创建SoundPool对象
SoundPool sp= new SoundPool(10,AudioManager.STREAM_SYSTEM,0);其中参数分别用于指定可以容纳多少个音频,声音类型,音
频的品质,0为默认值.//创建一个SoundPool对象,该对象可以容纳10个音频流
2.加载所要播放的音频(四种方法,目前只列出其中一种)
public int load(Context context,int resId,int priority)//用于通过指定的资源ID来加载音频,如sound.load(this,R.raw.ding,1);
3.播放音频
调用SoundPool对象的play()方法可播放指定音频
play(int soundID,float leftVolume,float rightVolume,int priority,float rate);//参数分别为要播放的音频,左声道音量,右声道音量,优先级,
循环次数,速率
sound.play(soundpool.load(this,R.raw.notify,1),1,1,0,0,1);
%%核心代码
soundpool = new SoundPool(5,
AudioManager.STREAM_SYSTEM, 0); //创建一个SoundPool对象,该对象可以容纳5个音频流
//将要播放的音频流保存到HashMap对象中
soundmap.put(1, soundpool.load(this, R.raw.chimes, 1));
soundmap.put(2, soundpool.load(this, R.raw.enter, 1));
soundmap.put(3, soundpool.load(this, R.raw.notify, 1));
soundmap.put(4, soundpool.load(this, R.raw.ringout, 1));
soundmap.put(5, soundpool.load(this, R.raw.ding, 1));
soundpool.play(soundmap.get(1), 1, 1, 0, 0, 1); //播放指定的音频