Thursday, February 5, 2009

A class for playing music and sound effects revisited

A while back, I published a class that could be used for playing music and sound effects. That class used the android.media.MediaPlayer class and was developed against a pre 1.0 android SDK version. The media player object is good for playing music, but it is not suitable for playing short sound effects. It is a heavyweight object and a bit unstable, too. But a new class called SoundPool (found in android.media package again) is available. This class is good for playing sound effects, but currently does not support .ogg or .mp3 files. Since I wanted my game to have both music and sound, I decided to design a class called SoundPoolManager which utilises both MediaPlayer and SoundPool objects. SoundPool objects are used to play sound effects and MediaPlayer objects are used to play back looping music.
SoundPoolManager creates a new thread and uses a LinkedList to queue sound events. So when you want to play a sound, you create a SoundPoolEvent and push it to the sound event queue. The SoundPoolManager thread picks up the queued events and plays them back for you. Time to show you some code:

 

This is the Sound interface used to abstract soundplaying functions.

package org.teacake.monolith.apk;
public interface Sound {

  public void addSound(int resid, boolean isLooping);
  public void startSound();
  public void stopSound();
 public void stopSound(int resid);
  public void playSound(int resid);
  public void startMusic(int resid);
  public void stopMusic(int resid);
  public void pauseMusic(int resid);
  public void resumeMusic(int resid);
}


And now for the main dish, SoundPoolManager class

package org.teacake.monolith.apk;
import android.media.SoundPool;
import android.util.Log;
class SoundPoolEvent
{
public SoundPoolEvent(int eventType,int eventSound)
{
this.eventType = eventType;
this.eventSound = eventSound;
}
public int eventType;
public int eventSound;

public static final int SOUND_PLAY=0;
public static final int SOUND_STOP=1;
public static final int SOUND_MUSIC_PLAY=2;
public static final int SOUND_MUSIC_PAUSE=3;
public static final int SOUND_MUSIC_STOP=4;
public static final int SOUND_MUSIC_RESUME=5;
}
class SoundStatus
{
public SoundStatus()
{

}
public static final int STATUS_LOOPING_NOT_STARTED=0;
public static final int STATUS_LOOPING_PAUSED=1;
public static final int STATUS_LOOPING_PLAYING=2;


}
public class SoundPoolManager extends Thread implements Sound
{
SoundPoolManager(android.content.Context context)
{
this.context = context;
soundEvents = new java.util.LinkedList();
sounds = new java.util.HashMap();
handles = new java.util.HashMap();
mediaPlayers = new java.util.HashMap();
isRunning = false;

}
public void addSound(int resid, boolean isLooping)
{

sounds.put(resid, new Boolean(isLooping));
if(isLooping)
{
try
{
android.media.MediaPlayer mp = android.media.MediaPlayer.create(context, resid);
mp.setLooping(true);
mp.seekTo(0);
//mp.prepare();
mediaPlayers.put(resid, mp);


//mp.seekTo(0);
//mp.setAudioStreamType(android.media.AudioManager.STREAM_MUSIC);
}
catch(Exception e)
{
Log.d("ERROR",e.getMessage());
}



}
}

public void run()
{
android.media.MediaPlayer mp = null;
while(isRunning)
{
try
{
while(soundEvents.size()>0)
{
SoundPoolEvent event = soundEvents.remove();
if(event!=null)
{
switch(event.eventType)
{
case SoundPoolEvent.SOUND_PLAY:
android.media.AudioManager mgr = (android.media.AudioManager) context.getSystemService(android.content.Context.AUDIO_SERVICE);
int streamVolume = mgr.getStreamVolume(android.media.AudioManager.STREAM_MUSIC);
soundPool.play(handles.get( event.eventSound).intValue(), streamVolume, streamVolume, 1, 0, 1.0f);

break;
case SoundPoolEvent.SOUND_STOP:

break;
case SoundPoolEvent.SOUND_MUSIC_PLAY:
currentPlayer = event.eventSound;
if(sounds.get(currentPlayer)!=null)
{
mp = mediaPlayers.get(currentPlayer);

if(!mp.isPlaying())
{
mp.seekTo(0);
mp.start();
}
}

break;
case SoundPoolEvent.SOUND_MUSIC_STOP:
currentPlayer = event.eventSound;
mp = mediaPlayers.get(currentPlayer);
mp.pause();
break;
case SoundPoolEvent.SOUND_MUSIC_PAUSE:
currentPlayer = event.eventSound;
mp = mediaPlayers.get(currentPlayer);
mp.pause();
break;
case SoundPoolEvent.SOUND_MUSIC_RESUME:
currentPlayer = event.eventSound;
mp = mediaPlayers.get(currentPlayer);
mp.start();
break;

}

}
}
}
catch(Exception e)
{
//Log.d("Error",e.getMessage());
}

try
{
this.wait(100);
//java.lang.Thread.currentThread().sleep(100);
}
catch(Exception e)
{

}
}
}


public void startSound()
{
this.soundPool = new android.media.SoundPool(this.sounds.size(),android.media.AudioManager.STREAM_MUSIC,100);
java.util.Iterator iterator = sounds.keySet().iterator();

while(iterator.hasNext())
{
int soundid = iterator.next().intValue();
int soundhandle = this.soundPool.load(this.context, soundid, 1);
handles.put(new Integer(soundid), new Integer(soundhandle));
}

isRunning=true;
this.start();
}
public void stopSound()
{
java.util.Iterator iterator = sounds.keySet().iterator();

while(iterator.hasNext())
{

int soundid = iterator.next().intValue();
if(this.sounds.get(soundid).booleanValue())
{
android.media.MediaPlayer mp = mediaPlayers.get(soundid);
mp.stop();
mp.release();
mp=null;
}
else
{
this.soundPool.pause( this.handles.get(soundid).intValue());
this.soundPool.stop(this.handles.get(soundid).intValue());

}


}

isRunning=false;
this.soundPool.release();
}

public int currentPlayer;
private boolean isRunning;
private java.util.HashMap sounds;
private java.util.HashMap handles;
private android.content.Context context;
private java.util.LinkedList soundEvents;
private java.util.HashMap mediaPlayers;
public void stopSound(int resid)
{
if(soundEvents!=null)
{
try
{
soundEvents.add(new SoundPoolEvent(SoundPoolEvent.SOUND_STOP,resid));
}
catch(Exception e)
{

}
}
}
public void playSound(int resid)
{
if(soundEvents!=null)
{
try
{
soundEvents.add(new SoundPoolEvent(SoundPoolEvent.SOUND_PLAY,resid));
this.notify();
}
catch(Exception e)
{

}
}
}
public void startMusic(int resid)
{
if(soundEvents!=null)
{
try
{
soundEvents.add(new SoundPoolEvent(SoundPoolEvent.SOUND_MUSIC_PLAY,resid));
this.notify();
}
catch(Exception e)
{

}
}
}
public void stopMusic(int resid)
{
if(soundEvents!=null)
{
try
{
soundEvents.add(new SoundPoolEvent(SoundPoolEvent.SOUND_MUSIC_STOP,resid));
this.notify();
}
catch(Exception e)
{

}
}
}
public void pauseMusic(int resid)
{
if(soundEvents!=null)
{
try
{
soundEvents.add(new SoundPoolEvent(SoundPoolEvent.SOUND_MUSIC_PAUSE,resid));
this.notify();
}
catch(Exception e)
{

}
}
}
public void resumeMusic(int resid)
{
if(soundEvents!=null)
{
try
{
soundEvents.add(new SoundPoolEvent(SoundPoolEvent.SOUND_MUSIC_RESUME,resid));
this.notify();
}
catch(Exception e)
{

}
}
}
SoundPool soundPool;


}


How do you use it?
First create an instance of the class.
Then add the resources to the SoundPoolManager (Sounds and music tracks)
Then start the SoundManager thread.
Then use the playSound() method to play a sound effect or playMusic() to play a song.

SoundPoolManager m = new SoundPoolManager(context);
m.addSound(R.raw.bang, false);
m.addSound(R.raw.mysong,true);
m.startSound();
m.playSound(R.raw.bang);
m.playMusic(R.raw.mysong);

If you want to pause the music use the pause call.

Use the stopSound() method to stop the sound thread.
The code is crude and the method names are not quite appropriate but it is published under the assumption that you may find it useful. You can take a look at the Robotic Space Rock code found at http://code.google.com/p/monolithandroid for actual examples.

10 comments:

Steve Henderson said...

Thank you! This saved me hours of struggling to get the mplayer to play sound effects.

Unknown said...

Useful class, great tutorial for me!
Can be effectively used with ndk

Sourav Agrawal said...

This is for making available the objects that are required by a beginner or an programming expert to go a level above in its skills, to modify the programming world in its own way. Best of Luck : VcubeS code (powered by VcubeS Planet)

www.vcubescode.blogspot.com

Stephenwilliams said...

I can see that you are putting a lots of efforts into your blog. Keep posting the good work.Some really helpful information in there. Bookmarked. Nice to see your site. I really appreciate your work! Awesome!
Classified php script

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.

Search the web