我如何知道 Android 中的声音是否播放完毕?
我如何知道声音是否已播放完毕?
我想播放 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 MediaPlayer 类和 OnCompletionListener
Use the MediaPlayer class and an OnCompletionListener
只是一个想法...获取 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.
抱歉,现在回复太晚了。但我使用媒体播放器中的 isPlaying() API 来等待第一首歌曲完成。
Sorry this is too late to reply. But i used isPlaying() API from mediaplayer to wait for first song to complete.
我解决这个问题的方法是创建一个包含图像、声音和延迟的动画序列。然后我可以将它们排队,这样我就可以显示图像并伴随声音,然后切换到另一个图像。我使用 SoundPlayer 和自定义视图(不是视图翻转器,尽管您可能可以)来执行此操作。
技巧是您不能简单地暂停线程以进行延迟,您需要使用处理程序向自己发送消息以便将来更新 UI。
这是处理程序
showNext() 方法只是获取序列中的下一个项目,更新图像视图,播放声音,然后安排一条消息供处理程序在序列之后调用 showNext。
当然,所有这些确实需要您确定延迟并对其进行硬编码,但根据我的经验,您无论如何都需要为游戏执行此操作。我在很多情况下都希望延迟比声音长。这是实际对项目进行排队的代码。
通过这种方法,我可以使用各种逻辑来添加动画项目、更改持续时间、循环动画等......只需进行一些微调即可获得正确的延迟。
希望这有帮助。
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
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.
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.
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.