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.

- 17,750
- 17
- 113
- 128

- 554
- 1
- 4
- 9
5 Answers
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;
-
15The SoundPool constructor is deprecated now, but since SoundPool.Builder requires API level 21, I guess I'm still using it. – Noumenon Dec 14 '14 at 00:25
-
The last two lines (the MediaPlayer thing) are not necessary. For the rest, it has been helpful. Thanks:) – voghDev Mar 27 '17 at 05:26
-
@voghDev , why would it not be necessary? – juztcode Aug 10 '20 at 09:01
-
1@TheFlash , after we set things to `null` doesn't the garbage collector do it's thing automatically? What's the point of calling release? – juztcode Aug 10 '20 at 09:01
-
When I tested it, I had to delay in time the soundPool.play(...) part. It did not work if you call the play just after the load. – jlguenego Jul 22 '22 at 14:23
-
IMHO you need to wait for loading the sample using onLoadListener and then start playback – Petr Nalevka Apr 11 '23 at 12:14
Yes. i went through this too. but for safety i have saved a piece of code i found online. Though havent used it, i know it will come in handy soon...
1) You need to create AudioAttributes object:
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
2) Create SoundPool object:
SoundPool sounds = new SoundPool.Builder()
.setAudioAttributes(attributes)
.build();
3) How to use SoundPool on all API levels example :
SoundPool sound;
protected void createSoundPool() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
createNewSoundPool();
} else {
createOldSoundPool();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected void createNewSoundPool(){
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
sounds = new SoundPool.Builder()
.setAudioAttributes(attributes)
.build();
}
@SuppressWarnings("deprecation")
protected void createOldSoundPool(){
sounds = new SoundPool(5,AudioManager.STREAM_MUSIC,0);
}

- 9,696
- 8
- 50
- 101

- 886
- 9
- 14
-
1Note: You want a .setMaxStreams(5) on the Builder in createNewSoundPool too if you want to have the same behavior, to play several sounds at the same time, as in the old one. Also the 5 could be a final int or something instead. – Erik Melkersson Dec 03 '16 at 15:06
Here is a small, working example of soundPool
, it is taken from here and slightly modified to match post 21 API's.
One thing to notice is maxStreams
, which indicates how many streams are allowed to run in parallel, if it is one(default), it can be removed from the builder.
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager extends Activity
{
static SoundPool soundPool;
static int[] sm;
public static void InitSound() {
int maxStreams = 1;
Context mContext = getApplicationContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
soundPool = new SoundPool.Builder()
.setMaxStreams(maxStreams)
.build();
} else {
soundPool = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0);
}
sm = new int[3];
// fill your sounds
sm[0] = soundPool.load(mContext, R.raw.sound_1, 1);
sm[1] = soundPool.load(mContext, R.raw.sound_2, 1);
sm[2] = soundPool.load(mContext, R.raw.sound_3, 1);
}
static void playSound(int sound) {
soundPool.play(sm[sound], 1, 1, 1, 0, 1f);
}
public final void cleanUpIfEnd() {
sm = null;
soundPool.release();
soundPool = null;
}
}

- 5,405
- 7
- 35
- 52
-
1
-
1
-
Plus one for a useful answer despite the amg line being completed unneeded (tested without it) – jk7 Apr 10 '18 at 00:33
-
1getApplicationContext(); causes an error. "Non static method cannot be referenced from static context" – soztrk Jun 18 '18 at 03:43
I have written a SoundPoolManager which can be used to load sound files and play as and when required. You can see it here.
Thanks.

- 7,880
- 19
- 63
- 102
-
2I find the SoundPoolManager code readable, but the benefit of using int instead of SoundPool is not clear to me. I'm guessing it is for convenience. It would help if you would explain this more fully. Does SoundPoolManager address shortcomings of SoundPool, make my code safer/more portable/more manageable/more efficient? If so how and why? Thanks in advance. – CODE-REaD Jul 19 '16 at 14:02
-
private final int NUMBER_OF_SIMULTANEOUS_SOUNDS = 5;
private final float LEFT_VOLUME_VALUE = 1.0f;
private final float RIGHT_VOLUME_VALUE = 1.0f;
private final int MUSIC_LOOP = 0;
private final int SOUND_PLAY_PRIORITY = 0;
private final float PLAY_RATE= 1.0f;
SoundPool soundPool;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
soundPool= new SoundPool.Builder()
.setMaxStreams(NUMBER_OF_SIMULTANEOUS_SOUNDS)
.build();
} else {
// Deprecated way of creating a SoundPool before Android API 21.
soundPool= new SoundPool(NUMBER_OF_SIMULTANEOUS_SOUNDS, AudioManager.STREAM_MUSIC, 0);
}
int soundId = soundPool.load(getApplicationContext(), R.raw.sound_1, 1);
soundPool.play(soundId , LEFT_VOLUME_VALUE , RIGHT_VOLUME_VALUE, SOUND_PLAY_PRIORITY , MUSIC_LOOP ,PLAY_RATE);

- 12,407
- 10
- 54
- 67

- 668
- 1
- 10
- 18