如何检测声音文件何时完成?

发布于 2024-12-03 14:28:11 字数 416 浏览 0 评论 0原文

我正在广播接收器活动中使用此代码播放声音文件:

notification.sound = Uri.parse("android.resource://emad.app/raw/seven_chimes");

我想检测此文件何时完成播放。

我尝试了这样的循环,但我认为这只对媒体播放器有好处,因为它总是 = false,即使我确保声音文件仍在播放:

/*
 * Stay here until the chime sound is finished.
 */
   while (manager.isMusicActive()){
   }

你能告诉我我应该使用什么来代替 manager.isMusicActive ()?

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.

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()){
   }

Can you show me what I should use instead of manager.isMusicActive()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好久不见√ 2024-12-10 14:28:11

您可以使用 MediaPlayer 类并添加一个 Completion 侦听器,以便在声音播放完毕时激活

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();

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();
萌能量女王 2024-12-10 14:28:11

不使用广播接收器,但这对我当前的项目有用,使用 Kotlin 跟踪音频文件何时完成播放。

playButton.setOnClickListener {

            if (isPlaying) {
                pauseSound()
            }else {
                playSound()
            }

            mediaPlayer.setOnCompletionListener {
                playButton.text = "Play"
                isPlaying = false
            }
        }

isPlaying 是布尔值,如果有人感到困惑,我会解释更多。希望它将来对其他人有帮助。快乐编码!

Not working with broadcast receiver but this is what worked for my current project using Kotlin to track when the audio file has finished playing.

playButton.setOnClickListener {

            if (isPlaying) {
                pauseSound()
            }else {
                playSound()
            }

            mediaPlayer.setOnCompletionListener {
                playButton.text = "Play"
                isPlaying = false
            }
        }

isPlaying is boolean, I will explain more if someone is confused. Hope it helps someone else in the future. Happy coding!

场罚期间 2024-12-10 14:28:11

尝试使用服务来播放音乐..
播放一次后,停止您的服务,您可以在 onDestroy() 服务方法上添加您的程序。
希望有帮助:)

try using service to play the music..
after it play once, then stop your service and you can add your program on the onDestroy() service method..
hope it helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文