Search code examples
androidmediasoundpool

Play sound using soundpool example


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.


Solution

  • 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;