我如何知道 Android 中的声音是否播放完毕?

发布于 2024-10-04 22:10:57 字数 161 浏览 3 评论 0原文

我如何知道声音是否已播放完毕?

我想播放 2 个声音,但我想播放一个声音,然后等到第一个声音完成后再开始第二个声音。

另外,如果我想在声音结束时做其他事情,例如在视图翻转器中显示下一个视图,我可以这样做吗?

现在我正在使用 SoundPool 来播放我的声音。

How do I know if a sound has finished playing?

I want to play 2 sounds but I want one sound to play and then wait until the 1st sound is done before the 2nd starts.

Also, if I wanted to do something else when the sound is finished like show he next view in a view flipper, could I do that?

Right now I'm using SoundPool to play my sounds.

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

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

发布评论

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

评论(4

诗酒趁年少 2024-10-11 22:10:57

使用 MediaPlayer 类和 OnCompletionListener

Use the MediaPlayer class and an OnCompletionListener

度的依靠╰つ 2024-10-11 22:10:57

只是一个想法...获取 ogg/wav 声音播放时间长度并使用您自己的 SoundObject 类和计时器成员。
在一些更新功能中更新它。
如果定时器> maxtime + 阈值,将其标记为已完成。

Just an idea... get ogg/wav sound play time length and use your own SoundObject class with timer member.
Update it in some update function.
If timer > maxtime + threshold, flag it as finished.

古镇旧梦 2024-10-11 22:10:57

抱歉,现在回复太晚了。但我使用媒体播放器中的 isPlaying() API 来等待第一首歌曲完成。

while(mediaPlyaer.isPlaying()){
}

Sorry this is too late to reply. But i used isPlaying() API from mediaplayer to wait for first song to complete.

while(mediaPlyaer.isPlaying()){
}
山田美奈子 2024-10-11 22:10:57

我解决这个问题的方法是创建一个包含图像、声音和延迟的动画序列。然后我可以将它们排队,这样我就可以显示图像并伴随声音,然后切换到另一个图像。我使用 SoundPlayer 和自定义视图(不是视图翻转器,尽管您可能可以)来执行此操作。

技巧是您不能简单地暂停线程以进行延迟,您需要使用处理程序向自己发送消息以便将来更新 UI。

这是处理程序

handler = new Handler() {

            /* (non-Javadoc)
             * @see android.os.Handler#handleMessage(android.os.Message)
             */
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == NEXT_ITEM_MSG) {
                    showNext();
                }
                else if (msg.what == SEQUENCE_COMPLETE_MSG) {
                    // notify the listener
                    if (animationCompleteListener != null) {
                        animationCompleteListener.onAnimationComplete();
                    }
                }
            }
        };

showNext() 方法只是获取序列中的下一个项目,更新图像视图,播放声音,然后安排一条消息供处理程序在序列之后调用 showNext。

private void showNext() {
        if (mRunning) {
            // Get the first item
            AnimatedSequenceItem item = currentSequence.getNextSequenceItem();
            if (item == null) {
                Message msg = handler.obtainMessage(SEQUENCE_COMPLETE_MSG);
                handler.sendMessage(msg);
                return;
            }
            // Show the first image
            showImage(item.getImageResId());
            // Play the sound
            int iSoundResId = item.getSoundResId();
            if (iSoundResId != -1) {
                playSound(iSoundResId);
            }

            // schedule a message to advance to next item after duration
            Message msg = handler.obtainMessage(NEXT_ITEM_MSG);
            handler.sendMessageDelayed(msg, item.getDuration());
        }
    }

当然,所有这些确实需要您确定延迟并对其进行硬编码,但根据我的经验,您无论如何都需要为游戏执行此操作。我在很多情况下都希望延迟比声音长。这是实际对项目进行排队的代码。

battleSequence.addSequenceItem(R.drawable.brigands, 
                   R.raw.beep, 
                   2000);
battleSequence.addSequenceItem(R.drawable.black,
                               winning ? R.raw.enemy_hit : R.raw.player_hit, 
                               1500);

通过这种方法,我可以使用各种逻辑来添加动画项目、更改持续时间、循环动画等......只需进行一些微调即可获得正确的延迟。

希望这有帮助。

The way I solved this is to create an animation sequence that contains an image, a sound, and a delay. I can then queue these up so I can have a image shown, and sound accompany it, and then switch to another image. I do this with a SoundPlayer and a custom view (not a view flipper, though you probably could)

The trick is that you can't simply pause the thread for the delay, you'll need to use a handler to send yourself a message to update the UI in the future.

Here is the Handler

handler = new Handler() {

            /* (non-Javadoc)
             * @see android.os.Handler#handleMessage(android.os.Message)
             */
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == NEXT_ITEM_MSG) {
                    showNext();
                }
                else if (msg.what == SEQUENCE_COMPLETE_MSG) {
                    // notify the listener
                    if (animationCompleteListener != null) {
                        animationCompleteListener.onAnimationComplete();
                    }
                }
            }
        };

The showNext() method simply grabs the next item in the sequence, updates the image view, plays the sound, and then schedules a message for the handler to call showNext after the sequence.

private void showNext() {
        if (mRunning) {
            // Get the first item
            AnimatedSequenceItem item = currentSequence.getNextSequenceItem();
            if (item == null) {
                Message msg = handler.obtainMessage(SEQUENCE_COMPLETE_MSG);
                handler.sendMessage(msg);
                return;
            }
            // Show the first image
            showImage(item.getImageResId());
            // Play the sound
            int iSoundResId = item.getSoundResId();
            if (iSoundResId != -1) {
                playSound(iSoundResId);
            }

            // schedule a message to advance to next item after duration
            Message msg = handler.obtainMessage(NEXT_ITEM_MSG);
            handler.sendMessageDelayed(msg, item.getDuration());
        }
    }

Of course all of this does require you to determine and hardcode the delays, but in my experience you need to do that for a game anyway. I had many cases where I wanted the delay to be longer than the sound. Here's the code that actually queues up items.

battleSequence.addSequenceItem(R.drawable.brigands, 
                   R.raw.beep, 
                   2000);
battleSequence.addSequenceItem(R.drawable.black,
                               winning ? R.raw.enemy_hit : R.raw.player_hit, 
                               1500);

With this approach I can have all sorts of logic to add animation items, change durations, loop animations, etc... It just took some fine tuning to get the delays right.

Hope this helps.

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