SpaceBeers
SpaceBeers

Reputation: 13947

Android SoundPool end event listener?

I've started messing about with Android recently. I'm trying to kick something off once a sound has finished and return to the previous activity.

spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        int soundID = spool.load(this, card.getSound(), 1);
        spool.play(soundID, 500, 500, 1, 0, 1f);

I can't see anything about getting the sound duration or any sort of sound end event. I know SoundPool can be a pain so I'm willing to ditch it.

How can I do something after a sound has ended? Can I do this with SoundPool or am I barking up the wrong tree?

Thanks. I'm pretty new to Android and Java so open to all suggestions.

Upvotes: 0

Views: 1792

Answers (1)

Fabian Tamp
Fabian Tamp

Reputation: 4526

If the multi-stream handling functionality of SoundPool isn't necessary, you can use MediaPlayer, which does have the callbacks you're asking about.

A rough example:

MediaPlayer mp = MediaPlayer.create(Activity.this, soundId);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        // do something
    }
});
mp.start();

Just make sure that you hang onto the MediaPlayer reference somewhere so that it doesn't get GC'd before your OnCompletionListener fires :)

Upvotes: 4

Related Questions