Android Development: Using SoundPool instead of MediaPlayer

I wanted to launch a simple app for Kindle Fire to test the Amazon market place for Android Apps. You can download the app here.

I chose one of our simplest sounds apps for Christmas. Here is a screenshot of the same.

The top buttons play the background music and the individual icons on the shelf play simple sounds.

My first struggle was with the layout and making it work with various other android devices. I will write an another post for that. But with regards to playing the actual sound files there were problems also.

There are 2 ways of playing sounds.

  1. MediaPlayer class
  2. SoundPool class

MediaPlayer:

Using media player is simple.


MediaPlayer backgroundMusicPlayer1;
backgroundMusicPlayer1 = MediaPlayer.create(this, R.raw.music1);
backgroundMusicPlayer1.setLooping(true);

And in order to play the music, just call


backgroundMusicPlayer1.start();

This is fine for playing a few files, but as you keep calling MediaPlayer.create(..), You will run out of memory and your app will crash. The better way to do this is using SoundPool class.

Instantiate SoundPool class



//Declaration
SoundPool soundPool;

//Initialization 
soundPool = new SoundPool(8, AudioManager.STREAM_MUSIC, 0);

The first parameter is number of simultaneous streams you can have. second is type of stream and third is quality. 3rd parameter has not been implemented as of this writing, so you can safely just put a zero.

Here is documentation for SoundPool: http://developer.android.com/reference/android/media/SoundPool.html

Unfortunately whenever a sound is loaded to the soundPool, it generates its own SoundID (An integer) that you have to keep track of. In my app, I just created a hashmap that takes cares of mapping the loaded soundIDs to Icon tags.


//Declaration
HashMap soundPlayers;

//Initialization
soundPlayers = new HashMap();


I load sounds dynamically when an icon is clicked. Here is my code.


btn.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {
     v.startAnimation(AnimationUtils.loadAnimation(ChristmasSoundsActivity.this, R.anim.image_click));
     if(soundPlayers.containsKey(v.getTag())){
         int sound = (int) soundPlayers.get(v.getTag());
         soundPool.play(sound, 1.0f, 1.0f, 1, 0, 1.0f);

     }
     else{
         int sound = soundPool.load(ChristmasSoundsActivity.this, getResources().getIdentifier("sound" + v.getTag(), "raw", "com.objectgraph.ChristmasSounds"), 1);
         soundPlayers.put(v.getTag().toString(), sound);

     }

 }
});

In Line6 as you can see, I am calling the soundPool.play

The first parameter is the soundID which i get from my hashmap. The parameters are listed below.


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

By: gavi on: