soundpool in android

We all know that we have MediaPlayer for playing sounds in android .So, why use  SoundPool ?

MediaPlayer and SoundPool both play sounds but to decide which one to use is based on the requirements of the application.

MediaPlayer is designed for longer sound files, this is best suited for music files or larger files. The files will be loaded from disk each time create is called, this will save on memory space but introduce a small delay .

Using MediaPlayer

MediaPlayer mp = MediaPlayer.create(YourClass.this, R.raw.sound);


Start or pause sound

mp.pause(); // Stop

mp.start();   // Start

Release resources and free memory


SoundPool Example

The code below depicts the usage of sound pool.

SoundPool mSoundPool;
HashMap<Integer, Integer> mSoundPoolMap;//creating a soundpool Hashmap
AudioManager provides access to volume and ringer mode control.
AudioManager mAudioManager
//Constructs a SoundPool object
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
// adding sounds to soundPool
// 1 is the index of sound
mSoundPoolMap.put(1, mSoundPool.load(this, R.raw.bottel, 1));
// 2 is the index of sound
mSoundPoolMap.put(2, mSoundPool.load(this,, 1));
//Return the handle to a system-level service by name
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//Returns the current volume index for a particular stream.
streamVolume =AudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//getting the max volume
float maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// play sound with index 1 from the sound pool
sound_stream1 =, streamVolume,streamVolume, 1, 0, 1f);
mSoundPool.stop(sound_stream1);//stop a sound from playing
mSoundPool.release();// free memory


1.Firstly ,create a SoundPool  objects and load them.

2.Then add these to the SoundPool hashmap object.

3.Play the sound from  mSoundPool by specifying the index of the sound.

4. Stop the sound as and when required.

5.Release sound resources in the end for better memory management.

The Hashmap used in line 2 is a  data structure consisting of a set of keys and values in which each key is mapped to a single value.

Line 6   mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100); creates a SoundPool object  .Its syntax is  SoundPool (int maxStreams, int streamType, int srcQuality)

  • maxStreams is the maximum number of simultaneous streams for this SoundPool object.
  • streamType is the  the audio stream type
  • srcQuality  the sample-rate converter quality

Line 19

sound_stream1,streamVolume,streamVolume, 1, 0, 1f);

It plays the sound specified by the sounded and has the following syntax:

public final int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)

where :

  • soundID -a soundID returned by the load() function
  • leftVolume-left volume value (range = 0.0 to 1.0)
  • rightVolume-right volume value (range = 0.0 to 1.0)
  • priority-stream priority (0 = lowest priority)
  • loop-loop mode (0 = no loop, -1 = loop forever)
  • rate-playback rate (1.0 = normal playback, range 0.5 to 2.0)

Now, you know , when and how to use MediaPlayer and SoundPool based on your applications requirements .