您如何检测声音文件何时完成? [英] How do you detect when a sound file has finished?
本文介绍了您如何检测声音文件何时完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在广播接收器活动中使用此代码播放声音文件:
I am playing a sound file using this code inside a broadcast receiver activity:
notification.sound = Uri.parse("android.resource://emad.app/raw/seven_chimes");
我想检测此文件何时播放完毕.
I would like to detect when this file has finished playing.
我尝试了这样的循环,但我认为这仅对媒体播放器有益,因为即使我确定声音文件仍在播放,它也总是 = false:
I tried a loop like this but I think this is only good for the media player because it always = false even though I made sure the sound file was still playing:
/*
* Stay here until the chime sound is finished.
*/
while (manager.isMusicActive()){
}
你能告诉我我应该用什么来代替 manager.isMusicActive()
?
Can you show me what I should use instead of manager.isMusicActive()
?
推荐答案
您可以使用 MediaPlayer 类并添加一个 Completion 侦听器以在声音播放完毕时激活
You can use the MediaPlayer class and add a Completion listener to be activated when the sound finishes playing
MediaPlayer mp = MediaPlayer.create(this,Uri.parse("android.resource://emad.app/raw/seven_chimes"));
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
performOnEnd();
}
});
mp.start();
这篇关于您如何检测声音文件何时完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文