Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SoundPool: get notified when end of played

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

like image 281
Jason Rogers Avatar asked Dec 14 '10 05:12

Jason Rogers


3 Answers

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

like image 163
Brent Watson Avatar answered Nov 20 '22 05:11

Brent Watson


It can't be done with SoundPool as far as I can tell.

The only audio 'player' that I know which can provide a completion notification is MediaPlayer - it's more of a complex beast than SoundPool but allows setting an OnCompletionListener to be notified when playback is complete.

like image 14
Squonk Avatar answered Nov 20 '22 06:11

Squonk


I have more than 100 short sound clips and SoundPool is my best option. I want to play one clip just after another clip is finished playing. Upon finding that there is no onCompletionListener() equivalent I chose to implement a runnable. This works for me because the first sound is between 1 and 2 seconds long so I have the duration of the runnable set at 2000. Hope they work on this class because its got lots of potential!

like image 7
Casey Murray Avatar answered Nov 20 '22 05:11

Casey Murray