Play sound using soundpool example

Sally Gomez picture Sally Gomez · Jun 12, 2013 · Viewed 84.7k times · Source

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.

Answer

TheFlash picture TheFlash · Jun 12, 2013

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;