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()返回)。
這裏需要注意的是,
1.play()函數傳遞的是一個load()返回的soundID——指向一個被記載的音頻資源 ,如果播放成功則返回一個非0的streamID——指向一個成功播放的流 ;同一個soundID 可以通過多次調用play()而獲得多個不同的streamID (只要不超出同時播放的最大數量);
2.pause()、resume()和stop()是針對播放流操作的,傳遞的是play()返回的streamID ;
3.play()中的priority參數,只在同時播放的流的數量超過了預先設定的最大數量是起作用,管理器將自動終止優先級低的播放流。如果存在多個同樣優先級的流,再進一步根據其創建事件來處理,新創建的流的年齡是最小的,將被終止;
4.無論如何,程序退出時,手動終止播放並釋放資源是必要的。
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.ir_begin, 1));
- soundMap.put(2, soundPool.load(MainActivity.this, R.raw.ir_end, 1));
- soundMap.put(3, soundPool.load(MainActivity.this, R.raw.ir_inter, 1));
- soundMap.put(