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

mp.start();

Start or pause sound

mp.pause(); // Stop

mp.start();   // Start

Release resources and free memory

mp.release();

SoundPool Example

The code below depicts the usage of sound pool.

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

Explanation:

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 =SoundPool.play(mSoundPoolMap.get(1),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 .