Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 634
SnoX Avatar asked Apr 01 '12 08:04

SnoX


People also ask

Which MediaPlayer callback method will notify us when the MediaPlayer has finished playing the audio file?

OnCompletionListener. Interface definition for a callback to be invoked when playback of a media source has completed.

How do I play music through MediaPlayer?

Start/Pause the playback: After loading the media file, you can start playing the media file by using the start() method. Similarly, you can pause the playing media file by using the pause() method. Stop the playback: You can stop playback by using the reset() method.

What is the use of MediaPlayer class?

MediaPlayer Class in Android is used to play media files. Those are Audio and Video files. It can also be used to play audio or video streams over the network.


1 Answers

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     } }); 
like image 198
MByD Avatar answered Sep 24 '22 04:09

MByD