開發Android應用中我們可能經常需要播放多媒體聲音文件,一般使用MediaPlayer類,但該類占用資源較多,對於游戲等應用可能不是很適合,SoundPool類在SDK的android.media.SoundPool,顧名思義是聲音池的意思。主要播放一些較短的聲音片段,可以從程序的資源或文件系統加載,相對於MediaPlayer類可以做到使用較少的CPU資源和較短的反應延遲。SoundPool和其他聲音播放類相比,其特點是可以自行設置聲音的品質、音量、播放比率等參等。並且它可以同時管理多個音頻流,每個流都有獨自的ID,對某個音頻流的管理都是通過ID進行的。
SoundPool基本使用方法:
1. 創建一個SoundPool
創建一個SoundPool對象:new SoundPool(int maxStreams, int streamType, int srcQuality)
public SoundPool(int maxStream, int streamType, int srcQuality)
maxStream —— 同時播放的流的最大數量
streamType —— 流的類型,一般為STREAM_MUSIC(具體在AudioManager類中列出)
srcQuality —— 采樣率轉化質量,當前無效果,使用0作為默認值
eg.
SoundPool soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
創建了一個最多支持3個流同時播放的,類型標記為音樂的SoundPool。
2、 從資源或者文件載入音頻流: load(Context context, int resId, int priority);
soundpool的加載:
int load(Context context, int resId, int priority) //從APK資源載入
int load(FileDescriptor fd, long offset, long length, int priority) //從FileDescriptor對象載入
int load(AssetFileDescriptor afd, int priority) //從Asset對象載入
int load(String path, int priority) //從完整文件路徑名載入
最后一個參數為優先級。
一般把多個聲音放到HashMap中去,比如
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(1, soundPool.load(this, R.raw.dingdong, 1));
3、 播放聲音play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate);
play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) ,
其中leftVolume和rightVolume表示左右音量,
priority表示優先級,
loop表示循環次數,
rate表示速率,如 //速率最低0.5最高為2,1代表正常速度
sp.play(soundId, 1, 1, 0, 0, 1);
而停止則可以使用 pause(int streamID) 方法,這里的streamID和soundID均在構造SoundPool類的
第一個參數中指明了總數量,而id從0開始。
注意事項:
1. SoundPool最大只能申請1M的內存空間,這就意味着我們只能用一些很短的聲音片段,而不是用它來播放歌曲或者做游戲背景音樂。
2. SoundPool提供了pause和stop方法,但這些方法建議最好不要輕易使用,因為有些時候它們可能會使你的程序莫名其妙的終止。Android開發網建議使用這兩個方法的時候盡可能多做測試工作,還有些朋友反映它們不會立即中止播放聲音,而是把緩沖區里的數據播放完才會停下來,也許會多播放一秒鍾。
3. SoundPool的效率問題。其實SoundPool的效率在這些播放類中算是很好的了,這可能會影響用戶體驗。也許這不能管SoundPool本身,因為到了性能比較好的Droid中這個延遲就可以讓人接受了。
4. 有的文件雖然很小,但是解碼出來為16位的PCM可能會超過1M的空間這時只能播放很短的部分聲音。文件超過1M的就播不了了。
下面直接上代碼

布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/StartButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="開始播放"
/>
<Button
android:id="@+id/PauseButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="暫停播放" />
<Button
android:id="@+id/StopButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止播放" />
</LinearLayout>
源代碼
package com.example.testsoundpool;
import java.util.HashMap;
import android.R.bool;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private SoundPool sp=null;//聲明一個SoundPool的引用
private HashMap <Integer,Integer> hm;//聲明一個HashMap來存放聲音資源
private int currentStreamId;//當前播放的StreamId
private Button startPlayButton;//播放按鈕
private Button PausePlayButton;//暫停按鈕
private Button stopPlayButton;//停止播放
private Boolean isFinishedLoad=false;//查看音樂文件是否加載完畢
private Boolean isPausePlay=false;//是否暫停播放
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//按鈕點擊響應函數
startPlayButton=(Button)findViewById(R.id.StartButton);
PausePlayButton=(Button)findViewById(R.id.PauseButton);
stopPlayButton=(Button)findViewById(R.id.StopButton);
startPlayButton.setOnClickListener(this);
PausePlayButton.setOnClickListener(this);
stopPlayButton.setOnClickListener(this);
//初始化SoundPool
initSoundPool();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//按鈕響應函數
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.StartButton:
playSound(1,0);//播放dudu,dudu文件被解碼為16位的PCM數據后超過了SoundPool的1M緩沖區了,循環不了,而且不能播完整個歌曲
//playSound(2,-1);//循環播放musictest
break;
case R.id.PauseButton:
isPausePlay=true;
sp.pause(currentStreamId);
break;
case R.id.StopButton:
isPausePlay=false;
sp.stop(currentStreamId);
break;
default:
System.out.println("switch default");
break;
}
}
public void initSoundPool(){
System.out.println("+initSoundPool+");
sp=new SoundPool(4,AudioManager.STREAM_MUSIC,0);//創建SoundPool對象
sp.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
// TODO Auto-generated method stub
isFinishedLoad=true;
System.out.println("setOnLoadCompleteListener");
}
});
hm=new HashMap<Integer,Integer>();//創建HashMap對象
hm.put(1, sp.load(this, R.raw.dudu, 0));//加載dudu聲音文件並設置為1號文件放入hm
hm.put(2, sp.load(this, R.raw.musictest, 0));//加載musictest聲音文件並設置為2號文件放入hm
System.out.println("-initSoundPool-");
}
//sound hm中的第幾個歌曲
//loop 是否循環 0不循環 -1循環
public void playSound(int sound,int loop){
String log;
if(!isPausePlay){
AudioManager am=(AudioManager)this.getSystemService(AUDIO_SERVICE);
float currentStreamVolume=am.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxStreamVolume=am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float setVolume=(float)currentStreamVolume/maxStreamVolume;
if(isFinishedLoad)
currentStreamId=sp.play(hm.get(sound), setVolume, setVolume, 1, loop, 1.0f);
log="playSound currentStreamId:"+String.valueOf(currentStreamId);
System.out.println(log);
}
else{
isPausePlay=false;
sp.resume(currentStreamId);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
sp.unload(currentStreamId);
sp.release();
}
}
