Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
956 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...