使用SoundPool播放音效
假设应用程序常常播放密集、急促而又短暂的音效(如游戏音效)那么使用显得有些不太适合了。由于存在例如以下缺点:
1) 延时时间较长,且资源占用率高。
2) 不支持多个音频同一时候播放。
Android中除了播放音频之外还提供了SoundPool来播放音效。SoundPool使用音效池的概念来管理多个短促的音效,比如它能够開始就载入20个音效。以后在程序中按音效的ID进行播放。
SoundPool主要用于播放一些较短的声音片段。与MediaPlayer相比,SoundPool的优势在于CPU资源占用量低和反应延迟小。
另外,SoundPool还支持自行设置声音的品质、音量、 播放比率等參数。
SoundPool提供了一个构造器,该构造器能够指定它总共支持多少个声音(也就是池的大小)、声音的品质等。构造器例如以下:
SoundPool(int maxStreams, int streamType, int srcQuality):第一个參数指定支持多少个声音;第二个參数指定声音类型:第三个參数指定声音品质。
一旦得到了SoundPool对象之后,接下来就可调用SoundPool的多个重载的load方法来载入声音了。
SoundPool提供了例如以下4个load方法:
int load(Context context, int resld, int priority):从 resld 所相应的资源载入声音。
int load(FileDescriptor fd, long offset, long length, int priority):载入 fd 所相应的文件的offset開始、长度为length的声音。
int load(AssetFileDescriptor afd, int priority):从afd 所相应的文件里载入声音。
int load(String path, int priority):从path 相应的文件去载入声音。
上面4个方法中都有一个priority參数,该參数眼下还没有不论什么作用,Android建议将该 參数设为1,保持和未来的兼容性。
上面4个方法载入声音之后,都会返回该声音的的ID,以后程序就能够通过该声音的ID 来播放指定声音。
SoundPool提供的播放指定声音的方法:
int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate):该方法的第一个參数指定播放哪个声音。leftVolume、rightVolume指定左、右的音量:priority指定播放声音的优先级,数值越大。优先级越高。loop指定是否循环,0为不循环,-1为循环。rate指定播放的比率,数值可从0.5到2, 1为正常比率。
为了更好地管理SoundPool所载入的每一个声音的1D,程序通常会使用一个HashMap<Integer,Integer>对象来管理声音。
归纳起来,使用SoundPool播放声音的过程例如以下:
1) 调用SoundPool的构造器创建SoundPool的对象。
2) 调用SoundPool对象的load()方法从指定资源、文件里载入声音。
最好使用HashMap< Integer, Integer>来管理所载入的声音。
3) 调用SoundPool的play方法播放声音。
以下的程序示范了怎样使用SoundPool来播放音效。
程序代码例如以下:
public class SoundPoolDemo extends Activity { Button btn1,btn2,btn3; //创建一个SoundPool对象 SoundPool soundPool; //定义一个HashMap用于存放音频流的ID HashMap<Integer, Integer>musicId=new HashMap<Integer, Integer>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mian); btn1=(Button)findViewById(R.id.btn1); btn2=(Button)findViewById(R.id.btn2); btn3=(Button)findViewById(R.id.btn3); //初始化soundPool,设置可容纳12个音频流,音频流的质量为5。 soundPool=new SoundPool(12, 0,5); //通过load方法载入指定音频流。并将返回的音频ID放入musicId中 musicId.put(1, soundPool.load(this, R.raw.awooga, 1)); musicId.put(2, soundPool.load(this, R.raw.evillaugh, 1)); musicId.put(3, soundPool.load(this, R.raw.jackinthebox, 1)); OnClickListener listener=new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn1: //播放指定的音频流 soundPool.play(musicId.get(1),1,1, 0, 0, 1); break; case R.id.btn2: soundPool.play(musicId.get(2),1,1, 0, 0, 1); break; case R.id.btn3: soundPool.play(musicId.get(3),1,1, 0, 0, 1); break; default: break; } } }; btn1.setOnClickListener(listener); btn2.setOnClickListener(listener); btn3.setOnClickListener(listener); } } |
程序执行效果图:
实际使用SoundPool播放声音时须要注意例如以下几点:
SoundPool尽管能够一次性载入多个声音。但因为内存限制,因此应该避免使用SoundPool来播放歌曲或者做游戏背景音乐。仅仅有那些短促、密集的声音才考虑使用SoundPool进行播放。
尽管SoundPool比MediaPlayer的效果好,但也不是绝对不存在延迟问题。尤其在那些性能不太好的手机中,SoundPool的延迟问题会更严重。