如何在 Windows 窗体应用程序中依次播放两种声音?
我想依次播放两种声音以响应按钮单击。当第一个声音结束时,第二个声音应该开始播放。
我的问题是,每次单击按钮时,这两个声音都是不同的,并且我不知道它们的长度以便使用 Thread.Sleep。但我不希望这些声音相互重叠播放。
I want to play two sounds one after the other in reaction to a button click. When the first sound finishes, the second sound should start playing.
My problem is that every time the button is clicked these two sounds are different and I don't know their lengths in order to use Thread.Sleep
. But I don't want these sounds to play on top of each other.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来好像您正在使用
SoundPlayer
类的PlaySync
方法..首先将其添加到顶部:然后编写这样的代码:
该类自 .NET 2.0 起可用,因此您应该拥有它。
Sounds like you're after the
PlaySync
method ofSoundPlayer
class.. first add this on top:Then have such code:
This class is available since .NET 2.0 so you should have it.
MediaPlayer 有 MediaEnded 事件。在事件处理程序中,只需启动新媒体,它们就会连续播放。
The MediaPlayer has MediaEnded event. In the event handler, just start the new media and they should play back to back.
在 Shadow Wizards 的例子中,没有必要每次都创建一个新的声音播放器。
这也有效:
In Shadow Wizards example, it's not necessary to make a new soundplayer each time.
This also works:
使用 NAudio 的示例
Example Using NAudio