Android Sound Pool

09 Jul

Sound pool is used to play short sound clips say like gun shots during a game (something like less than 3 seconds long). A nice feature that sound pool comes with is that you can play many sounds simultaneously which happens a lot when you think of a game.
So you first build a Sound Pool object:

sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
shot = sp.load(this, R.raw.gunshot, 1);

The constructor for SoundPool takes the following parameters:

SoundPool(int maxStreams, int streamType, int srcQuality)

MaxStreams, is an int controls the number of audio being played at the same time. I don’t want to go so much into detail just want to show it was implemented. Visit the android developers page to get an in depth explanation of every parameter. But most games STREAM_MUSIC is used as the stream type.
shot is an integer(int). It will contain the location(R.raw.gunshot) of the sound in this case gun shots. This is how you load sound from the resource folder.

To play the sound:, 1, 1, 0,0, 1);

The soundPool object has a method play that takes the following parameters:

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

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)

After playing the sound you may want to release all the memory and resources the sound pool object uses.


And so the whole code will make more sense:(Btw check out the blog post on MediaPlayer this code has both ways for playing sound)

package com.learning.gilo;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;

public class Shoot extends Activity implements OnClickListener, OnLongClickListener{
	SoundPool sp;
	MediaPlayer mp;
	int shot = 0;
	public void onCreate(Bundle b){
		View v=  new View(this);
		sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
		shot = sp.load(this, R.raw.gunshot, 1);
		mp = MediaPlayer.create(this, R.raw.bark);
	public void onClick(View v){
		if(shot != 0), 1, 1, 0,0, 1);
	public boolean onLongClick(View v){
		return false;



Posted by on July 9, 2012 in Android, Android tutorials, Code


2 responses to “Android Sound Pool

  1. Meera

    October 30, 2012 at 11:51 am

    Thanks for the extreamly useful post .
    To decide whether we should used SoundPool or MediaPlayer is also critical. SoundPool is preferred when soundstreams are very short and plentyful otherwise we prefer MediaPlayer.Here is another useful blog putting light on this issue.

  2. Arnold

    July 15, 2013 at 4:36 pm

    I get an error on the method load(Context int int ) in type sound pool not applicble for the argurment ().


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: