3

I am playing a song with the help of Media Player using following code,

mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource( "sound_file_filepath" );
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() 
{
    @Override
    public void onPrepared(MediaPlayer mp) 
    {
        mediaPlayer.start();
    }
}); 

Now i have a toggle kind of image in my .xml file which first shows a play image. After clicking/touching the play image it will start the song and replace the image with stop image.

Now my question is how can i know that my sound has finished playing ? I am very well aware of isPlaying() method, it returns true if song is running and false if not. But when & where should i check this condition to display the play image again ?

Community
  • 1
  • 1
Raynold
  • 443
  • 2
  • 9
  • 28

6 Answers6

19

I believe something like this can help,

mediaPlayer.setOnCompletionListener(this);
// When song is ended then media player automatically called onCompletion method.
    public void onCompletion(MediaPlayer arg0) 
{
    // Write your code
}
Marko Niciforovic
  • 3,561
  • 2
  • 21
  • 28
0

Like OnPrepared Listener u have OnCompletion Listener like below

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub

        }
    });
nidhi_adiga
  • 1,114
  • 1
  • 16
  • 27
0

check this one:

mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        Log.i("Completion Listener","Song Complete");
    }
});
Spontifixus
  • 6,570
  • 9
  • 45
  • 63
SubbaReddy PolamReddy
  • 2,083
  • 2
  • 17
  • 23
0
MediaPlayer mp = new MediaPlayer();

mp.setOnCompletionListener(new OnCompletionListener() {     
    @Override
    public void onCompletion(MediaPlayer mp) {
        //Do the work after completion of audio
    }
});
Maarkoize
  • 2,601
  • 2
  • 16
  • 34
0

If you for some reason need another solution, as I did. This might work for you:

    if(mPlayer.getCurrentPosition() == mPlayer.getDuration()){
        // Do stuff here
    }
John T
  • 814
  • 10
  • 17
-1

Use the following code syntax-

Audio_object=document.getElementById('audio_tag_id');
if(Audio_object.currentTime==Audio_object.duration)
//the audio object has completely played.

audio_tag_id is the id of the <audio>-element.

Rajesh Paul
  • 6,793
  • 6
  • 40
  • 57