Android自助餐之SoundPool

Android自助餐之SoundPool

查看全套目錄

SoundPool介紹

  1. SoundPool相對於MediaPlayer,用前者播放短但反應速度要求高的聲音以及同時播放多個聲音。
  2. SoundPool使用獨立的線程載入聲音,SoundPool.OnLoadCompleteListener回調載入完成後的方法。

主要方法

  1. 加載聲音。返回值爲soundID。
    priority參數無效,置1
    若文件大於1M,則只加載前1M

    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加載
  2. 播放控制。

    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()返回)。
  3. 釋放資源。

    final boolean unload(int soundID) //卸載一個指定的音頻資源.
    final void release() //釋放SoundPool中的所有音頻資源.

使用方法

  1. 構造聲音池

    SoundPool soundPool=new SoundPool(5,AudioManager.STREAM_MUSIC,0);
  2. 準備聲音集

    Map<Integer, Integer> 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(4, soundPool.load(MainActivity.this, R.raw.tada, 1));
    soundMap.put(5, soundPool.load(MainActivity.this, R.raw.zhong, 1));
  3. 播放一個音頻

    soundPool.play(soundMap.get(1), 1, 1, 0, 0, 1);

使用示例

 public class MainActivity extends Activity {
    //1.準備資源文件。在res資源目錄中新建一個raw子目錄,將需要加載的音頻文件放入其中,比如加載了shiqishidaibeijingyinyue.mp3
    private SoundPool soundPool;
    private int soundId;
    private boolean flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //2.實現soundPool對象
        soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0); //分別對應聲音池數量,AudioManager.STREAM_MUSIC 和 0

        //3.使用soundPool加載聲音,該操作位異步操作,如果資源很大,需要一定的時間
        soundId = soundPool.load(this, R.raw.shiqishidaibeijingyinyue, 1);

        //4.爲聲音池設定加載完成監聽事件
        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                flag = true;    //  表示加載完成
            }
        });
    }
    //5.播放操作
    public void click(View view){
        if(flag){
            //  播放聲音池中的文件, 可以指定播放音量,優先級 聲音播放的速率
            soundPool.play(soundId, 1.0f, 0.5f, 1, 0, 1.0f);
        }else{
            Toast.makeText(this, "正在加載,請稍後", 1).show();
        }
    }

    //6.使用完全後,應該釋放資源
    @Override
    protected void onDestroy() {
        soundPool.release();
        soundPool = null;
        super.onDestroy();
    }
 }
發佈了68 篇原創文章 · 獲贊 49 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章