Search code examples
androidmedia-playerlistener

OnCompletion listener with MediaPlayer


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?


Solution

  • 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
        }
    });