如何检测歌曲播放完成? [英] How to detect song playing is completed?
问题描述
我正在使用以下代码在媒体播放器的帮助下播放歌曲,
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();
}
});
现在我的 .xml 文件中有一个切换类型的图像,它首先显示一个播放图像.单击/触摸播放图像后,它将开始播放歌曲并将图像替换为停止图像.
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.
现在我的问题是我怎么知道我的声音已经播放完毕?我非常清楚 isPlaying() 方法,如果歌曲正在运行,它返回 true,否则返回 false.但是当 &我应该在哪里检查这个条件以再次显示播放图像?
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 ?
推荐答案
我相信这样的事情会有所帮助,
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
}
这篇关于如何检测歌曲播放完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!