Android SoundPool: get notified when end of played

Jason Rogers picture Jason Rogers · Dec 14, 2010 · Viewed 13.3k times · Source

This sound so simple that I can't figure out why I can't find the answer lol

I have a working sound pool class (thanks to a tutorial and some tweaking I did), and it works fine.

the problem now is that I want to be able to change my background music randomly. (not always have the same music in a loop but have 2 or 3 and when one finishes I play one of the 2 others).

problem is I can't find a way to get notified that the music has finished playing.

Any ideas ?

Jason

Answer

Brent Watson picture Brent Watson · Feb 29, 2012

This is what I do:

On startup I get the length of each sound-click using a MediaPlayer:

private long getSoundDuration(int rawId){
   MediaPlayer player = MediaPlayer.create(context, rawId);
   int duration = player.getDuration();
   return duration;
}

and store the sound plus the duration together (in a DTO-type object).