您的位置:首页 > 其它

SoundPool的使用

2016-04-13 10:27 375 查看
在Android中可以使用MediaPlayer和SoundPool来播放声音。如果应用程序经常播放密集、急促而又短暂的音效(如游戏音效)那么使用 MediaPlayer 显得有些不太适合了。因为 MediaPlayer 存在如下缺点:

1) 延时时间较长,且资源占用率高。

2) 不支持多个音频同时播放。

而 SoundPool 使用音效池的概念来管理多个短促的音效,例如它可以开始就加载 20 个音效,以后在程序中按音效的 ID 进行播放。SoundPool 主要用于播放一些较短的声音片段,与 MediaPlayer 相比, SoundPool 的优势在于 CPU 资源占用量低和反应延迟小。另外, SoundPool 还支持自行设置声音的品质、音量、 播放比率等参数。

SoundPool 使用场景: 适合短促且对反应速度比较高的情况,如游戏音效或按键声等。下面介绍SoundPool的创建过程:

1. 创建一个SoundPool 对象

public SoundPool(int maxStream, int streamType, int srcQuality)

maxStream —— 同时播放的流的最大数量

streamType —— 流的类型,一般为STREAM_MUSIC(具体在AudioManager类中列出)

srcQuality —— 采样率转化质量,当前无效果,使用0作为默认值

初始化一个实例:

SoundPool soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);


创建了一个最多支持5个流同时播放的,类型标记为音乐的SoundPool。

2. 加载音频资源

可以通过四种途径来记载一个音频资源:

int load(AssetFileDescriptor afd, int priority)

通过一个AssetFileDescriptor对象

int load(Context context, int resId, int priority)

通过一个资源ID

int load(String path, int priority)

通过指定的路径加载

int load(FileDescriptor fd, long offset, long length, int priority)

通过FileDescriptor加载

*API中指出,其中的priority参数目前没有效果,建议设置为1。

一个SoundPool能同时管理多个音频,所以可以通过多次调用load函数来记载,如果记载成功将返回一个非0的soundID ,用于播放时指定特定的音频。

int soundID1 = soundPool.load(this, R.raw.sound1, 1);
if(soundID1 ==0){
// 记载失败
}else{
// 加载成功
}
int soundID2 = soundPool.load(this, R.raw.sound2, 1);


这里加载了两个流,并分别记录了返回的soundID 。

需要注意的是,

流的加载过程是一个将音频解压为原始16位PCM数据的过程,由一个后台线程来进行处理异步,所以初始化后不能立即播放,需要等待一点时间。

3. 播放控制

有以下几个函数可用于控制播放:

final int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)

播放指定音频的音效,并返回一个streamID 。

priority —— 流的优先级,值越大优先级高,影响当同时播放数量超出了最大支持数时SoundPool对该流的处理;

loop —— 循环播放的次数,0为值播放一次,-1为无限循环,其他值为播放loop+1次(例如,3为一共播放4次).

rate —— 播放的速率,范围0.5-2.0(0.5为一半速率,1.0为正常速率,2.0为两倍速率)

final void pause(int streamID)

暂停指定播放流的音效(streamID 应通过play()返回)。

final void resume(int streamID)

继续播放指定播放流的音效(streamID 应通过play()返回)。

final void stop(int streamID)

终止指定播放流的音效(streamID 应通过play()返回)。

这里需要注意的是,

①play()函数传递的是一个load()返回的soundID——指向一个被记载的音频资源 ,如果播放成功则返回一个非0的streamID——指向一个成功播放的流 ;同一个soundID 可以通过多次调用play()而获得多个不同的streamID (只要不超出同时播放的最大数量);

②pause()、resume()和stop()是针对播放流操作的,传递的是play()返回的streamID ;

③play()中的priority参数,只在同时播放的流的数量超过了预先设定的最大数量是起作用,管理器将自动终止优先级低的播放流。如果存在多个同样优先级的流,再进一步根据其创建事件来处理,新创建的流的年龄是最小的,将被终止;

④无论如何,程序退出时,手动终止播放并释放资源是必要的。

4. 更多属性设置

其实就是paly()中的一些参数的独立设置:

final void setLoop(int streamID, int loop)

设置指定播放流的循环.

final void setVolume(int streamID, float leftVolume, float rightVolume)

设置指定播放流的音量.

final void setPriority(int streamID, int priority)

设置指定播放流的优先级,上面已说明priority的作用.

final void setRate(int streamID, float rate)

设置指定播放流的速率,0.5-2.0.

5. 释放资源

可操作的函数有:

final boolean unload(int soundID)

卸载一个指定的音频资源.

final void release()

释放SoundPool中的所有音频资源.

下面对以上进行总结:

一个SoundPool可以:

1.管理多个音频资源,通过load()函数,成功则返回非0的soundID;

2.同时播放多个音频,通过play()函数,成功则返回非0的streamID;

3.pause()、resume()和stop()等操作是针对streamID(播放流)的;

4.当设置为无限循环时,需要手动调用stop()来终止播放;

5.播放流的优先级(play()中的priority参数),只在同时播放数超过设定的最大数时起作用;

6.程序中不用考虑(play触发的)播放流的生命周期,无效的soundID/streamID不会导致程序错误。

实例:

下面是一个具体的实现实例,用到的音频文件都是Windows自带的音频文件,源代码如下:

public class MainActivity extends Activity {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private SoundPool soundPool;
private Map<Integer, Integer> soundMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button4=(Button)findViewById(R.id.button4);
button5=(Button)findViewById(R.id.button5);
//创建一个SoundPool对象,该对象可以容纳5个音频流
soundPool=new SoundPool(5,AudioManager.STREAM_MUSIC,0);

soundMap=new HashMap<Integer, Integer>();
soundMap.put(1, soundPool.load(MainActivity.this, R.raw.t1, 1));
soundMap.put(2, soundPool.load(MainActivity.this, R.raw.t2, 1));
soundMap.put(3, soundPool.load(MainActivity.this, R.raw.t3, 1));
soundMap.put(4, soundPool.load(MainActivity.this, R.raw.t4, 1));
soundMap.put(5, soundPool.load(MainActivity.this, R.raw.t5, 1));
button1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
soundPool.play(soundMap.get(1), 1, 1, 0, 0, 1);
}
});
button2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
soundPool.play(soundMap.get(2), 1, 1, 0, 0, 1);
}
});
button3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
soundPool.play(soundMap.get(3), 1, 1, 0, 0, 1);
}
});
button4.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
soundPool.play(soundMap.get(4), 1, 1, 0, 0, 1);
}
});
button5.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
soundPool.play(soundMap.get(5), 1, 1, 0, 0, 1);
}
});
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

soundPool.play(soundMap.get(5), 1, 1, 0, 0, 1);
return super.onKeyDown(keyCode, event);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: