使用.net异步播放wav文件多次

发布于 2024-10-08 00:40:32 字数 151 浏览 7 评论 0原文

我有一个应用程序,我需要在按下某个键时播放 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 技术交流群。

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

发布评论

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

评论(4

赠我空喜 2024-10-15 00:40:33

将 Windows API 中的 PlaySoundSND_ASYNCSND_NOSTOP 标志结合使用。

http://www.pinvoke.net/default.aspx/winmm.playsound

使用

//And the actual usage
PlaySound (fileName, UIntPtr.Zero, (uint)(SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC | SoundFlags.SND_NOSTOP));

声明

[Flags]
public enum SoundFlags
{
    /// <summary>play synchronously (default)</summary>
    SND_SYNC = 0x0000,    
    /// <summary>play asynchronously</summary>
    SND_ASYNC = 0x0001,
    /// <summary>silence (!default) if sound not found</summary>
    SND_NODEFAULT = 0x0002,
    /// <summary>pszSound points to a memory file</summary>
    SND_MEMORY = 0x0004,
    /// <summary>loop the sound until next sndPlaySound</summary>
    SND_LOOP = 0x0008,    
    /// <summary>don't stop any currently playing sound</summary>
    SND_NOSTOP = 0x0010,
    /// <summary>Stop Playing Wave</summary>
    SND_PURGE = 0x40,
    /// <summary>don't wait if the driver is busy</summary>
    SND_NOWAIT = 0x00002000,
    /// <summary>name is a registry alias</summary>
    SND_ALIAS = 0x00010000,
    /// <summary>alias is a predefined id</summary>
    SND_ALIAS_ID = 0x00110000,
    /// <summary>name is file name</summary>
    SND_FILENAME = 0x00020000,
    /// <summary>name is resource name or atom</summary>
    SND_RESOURCE = 0x00040004
}

[DllImport("winmm.dll", SetLastError=true)]
static extern bool PlaySound(string pszSound, UIntPtr hmod, uint fdwSound);

更新

抱歉,您是对的。该 API 不能用于同时播放声音。您应该使用 waveOut API。看这篇文章: http://www.codeproject.com/KB/audio-视频/cswavrec.aspx

Use PlaySound from the windows API in combination with the SND_ASYNC and SND_NOSTOP flags.

http://www.pinvoke.net/default.aspx/winmm.playsound

Usage

//And the actual usage
PlaySound (fileName, UIntPtr.Zero, (uint)(SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC | SoundFlags.SND_NOSTOP));

Declarations

[Flags]
public enum SoundFlags
{
    /// <summary>play synchronously (default)</summary>
    SND_SYNC = 0x0000,    
    /// <summary>play asynchronously</summary>
    SND_ASYNC = 0x0001,
    /// <summary>silence (!default) if sound not found</summary>
    SND_NODEFAULT = 0x0002,
    /// <summary>pszSound points to a memory file</summary>
    SND_MEMORY = 0x0004,
    /// <summary>loop the sound until next sndPlaySound</summary>
    SND_LOOP = 0x0008,    
    /// <summary>don't stop any currently playing sound</summary>
    SND_NOSTOP = 0x0010,
    /// <summary>Stop Playing Wave</summary>
    SND_PURGE = 0x40,
    /// <summary>don't wait if the driver is busy</summary>
    SND_NOWAIT = 0x00002000,
    /// <summary>name is a registry alias</summary>
    SND_ALIAS = 0x00010000,
    /// <summary>alias is a predefined id</summary>
    SND_ALIAS_ID = 0x00110000,
    /// <summary>name is file name</summary>
    SND_FILENAME = 0x00020000,
    /// <summary>name is resource name or atom</summary>
    SND_RESOURCE = 0x00040004
}

[DllImport("winmm.dll", SetLastError=true)]
static extern bool PlaySound(string pszSound, UIntPtr hmod, uint fdwSound);

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.aspx

逆蝶 2024-10-15 00:40:33

NAudio 可能会提供您所需要的。我自己没用过,但是比较流行。

NAudio might offer what you need. I haven't used it myself, but it's relatively popular.

筑梦 2024-10-15 00:40:33

有一个名为 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.

梦与时光遇 2024-10-15 00:40:33

如果您问“如何同时播放多个声音”,那么 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.

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