開發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中這個延遲就可以讓人接受了。