OnCompletion listener with MediaPlayer

SnoX picture SnoX · Apr 1, 2012 · Viewed 38.5k times · Source

How do i use the OnCompletion listener for some music? I would like to press a button to go to another activity that plays some music and then goes back when the music playback is finished. I allready coded the other stuff. I just cant figure out how to use the OnCompletion listener?

Answer

MByD picture MByD · Apr 1, 2012

You should put the code that should be run when the music is completed in the OnCompletionListener, for example:

mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        finish(); // finish current activity
    }
});