如何检测播放歌曲完成? [英] How to detect song playing is completed?
问题描述
我玩一首歌与媒体播放器的帮助下使用以下code,
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。但是,当和放大器;我应该在哪里检查此条件,再次显示播放图像?
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屋!