Android之SoundPool详细讲解

来源:互联网 发布:linux系统数据库备份 编辑:程序博客网 时间:2024/05/21 01:28

SoundPool类支持同时播放多个音效,这对于游戏来说是十分必要的,而MediaPlayer类是同步执行的只能一个文件一个文件的播放。

-------------------------------------------------------------------------

一. 创建一个SoundPool (构造函数)

public SoundPool(int maxStream, intstreamType, int srcQuality) 
maxStream —— 同时播放的流的最大数量
streamType —— 流的类型,一般为STREAM_MUSIC(具体在AudioManager类中列出)
srcQuality —— 采样率转化质量,当前无效果,使用0作为默认值

 

初始化一个实例:
SoundPool soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0); 
创建了一个最多支持2个流同时播放的,类型标记为音乐的SoundPool。

SDK21版本后,SoundPool的创建发生了很大变化,所以在开发中需要进行版本控制。

代码举例:

//实例化SoundPool
if(Build.VERSION.SDK_INT>=21){
   
Log.e("SoundPool","onFalling: SDK_INT>=21");
   
SoundPool.Builder builder =newSoundPool.Builder();
   
builder.setMaxStreams(2);                                                          //传入最多播放音频数量,
    //AudioAttributes
是一个封装音频各种属性的方法
    AudioAttributes.Builder attrBuilder =newAudioAttributes.Builder();
   
attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);                        //设置音频流的合适的属性
   
builder.setAudioAttributes(attrBuilder.build());                                   //加载一个AudioAttributes
   
soundPool =builder.build();
}else{
   
Log.e("SoundPool","onFalling: SDK_INT<21");
   
soundPool =newSoundPool(2,AudioManager.STREAM_MUSIC,0);
}

-------------------------------------------------------------------------

二.加载音频资源

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

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数据的过程,由一个后台线程来进行处理异步,所以初始化后不能立即播放,需要等待一点时间。

-------------------------------------------------------------------------

三.播放控制

有以下几个函数可用于控制播放:
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()返回)。

 

这里需要注意的是, 
  1.play()函数传递的是一个load()返回的soundID——指向一个被记载的音频资源 ,如果播放成功则返回一个非0的streamID——指向一个成功播放的流 ;同一个soundID 可以通过多次调用play()而获得多个不同的streamID (只要不超出同时播放的最大数量);
  2.pause()、resume()和stop()是针对播放流操作的,传递的是play()返回的streamID ;
  3.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.

五.释放资源

可操作的函数有:
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不会导致程序错误。

-------------------------------------------------------------------------

 简单代码如下:

private int voiceId;                            //声音IDprivate int voiceStreamId;                      //播放流的ID,private SoundPool soundPool;                    //播放器//实例化SoundPoolif(Build.VERSION.SDK_INT>=21){    Log.e("SoundPool", "onFalling: SDK_INT>=21" );    SoundPool.Builder builder = new SoundPool.Builder();    builder.setMaxStreams(2);                                                           //传入最多播放音频数量,    //AudioAttributes是一个封装音频各种属性的方法    AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();    attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);                         //设置音频流的合适的属性    builder.setAudioAttributes(attrBuilder.build());                                    //加载一个AudioAttributes    soundPool = builder.build();}else{    Log.e("SoundPool", "onFalling: SDK_INT<21" );    soundPool = new SoundPool(2,AudioManager.STREAM_MUSIC,0);}/* 流的加载过程是一个将音频解压为原始16位PCM数据的过程,* 由一个后台线程来进行处理异步,* 所以初始化后不能立即播放,需要等待一点时间。*///加载voicevoiceId = soundPool.load(getApplicationContext(),R.raw.broken,1);播放并记录声音流IDvoiceStreamId = soundPool.play(voiceId,1,1,1,-1,1);                                 ///* 根据需要自行添加soundPool.resume(voiceStreamId);soundPool.pause(voiceStreamId);                                                                soundPool.stop(voiceStreamId);*/ 


原创粉丝点击