Play sound using soundpool example

后端 未结 5 1469
旧时难觅i
旧时难觅i 2020-11-27 14:51

I would like to learn how to use soundpool method. I would like you to show me a very simple example that run 2 sounds.

5条回答
  •  眼角桃花
    2020-11-27 15:14

    Create a folder named as raw under your_app/res/. Then paste your ringtone in this folder, for example your_app/res/raw/ringtone.mp3. Now use the following code:

    SoundPool soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    int soundId = soundPool.load(context, R.raw.ringtone, 1);
    // soundId for reuse later on
    
    soundPool.play(soundId, 1, 1, 0, 0, 1);
    

    Be sure to release the SoundPool resources after use:

    soundPool.release();
    soundPool = null;
    

提交回复
热议问题