使用.net异步播放wav文件多次
我有一个应用程序,我需要在按下某个键时播放 wav 文件,我使用 SoundPlayer 类,但是如果按下新键时正在播放另一个声音,它会停止声音并再次播放,使其看起来很难看。 ...
即使有另一个正在播放的声音,有什么办法可以再次播放相同的声音吗?
谢谢!
I have an app that I need to play a wav file when a key is pressed, I use the SoundPlayer class but if another sound is being played when a new key is pressed, it stops the sound the play it again making it look ugly....
is there any way to play the same sound again even if there is another of being played?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 Windows API 中的
PlaySound
与SND_ASYNC
和SND_NOSTOP
标志结合使用。http://www.pinvoke.net/default.aspx/winmm.playsound
使用
声明
更新
抱歉,您是对的。该 API 不能用于同时播放声音。您应该使用
waveOut
API。看这篇文章: http://www.codeproject.com/KB/audio-视频/cswavrec.aspxUse
PlaySound
from the windows API in combination with theSND_ASYNC
andSND_NOSTOP
flags.http://www.pinvoke.net/default.aspx/winmm.playsound
Usage
Declarations
Update
Apologies, you are correct. The API cannot be used to play sounds simultaneously. You should be using the
waveOut
api. Look at this article: http://www.codeproject.com/KB/audio-video/cswavrec.aspxNAudio 可能会提供您所需要的。我自己没用过,但是比较流行。
NAudio might offer what you need. I haven't used it myself, but it's relatively popular.
有一个名为 Irrklang 的免费(用于非商业应用程序).NET 声音库,支持播放多种声音(至少是我能收集到的 wav 和 mp3)同时,他们的 教程 特别涵盖了这种情况作为库的基本用法。
There's a free (for non-commercial applications) sound library for .NET called Irrklang that supports playing multiple sounds (at least wav and mp3 from what I can gather) at the same time, their tutorial specifically covers this case as basic usage of the library.
如果您问“如何同时播放多个声音”,那么 SoundPlayer 永远不会是答案。
我相信 PlaySound 也同样受到限制。
您可以查看这个问题和此问题了解有关声音 API 的更多选项。我快速浏览了 SDL 和 SDL Mixer,认为 SDL 太原始(您必须自己混合声音),而 SDL Mixer 太重量级(这就是所有这些和一袋芯片 - 无限混音和音乐通道(mp3、ogg、midi 等)。我看不到 BASS 的 C# 绑定的在线参考,但它对于非商业用途是免费的。
If you're asking, "how can I play multiple sounds at once" then SoundPlayer will never be the answer.
PlaySound, I believe, is also similarly limited.
You might look at this question and this question for more options on sound APIs. I took a quick look at SDL and SDL Mixer and thought that SDL was too primitive (you have to mix the sounds yourself) and SDL Mixer was too heavyweight (it's all that and a bag of chips - unlimited channels of mixing and music (mp3, ogg, midi, etc)). I couldn't see an online reference for BASS's C# bindings, but it's free for non-commercial use.