wpf 使用 System.Media.SoundPlayer 播放 Windows 系统声音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22123899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Playing Windows System Sounds with System.Media.SoundPlayer
提问by SchroedingersCat
Is it possible that the System.Media.SoundPlayercan not play Windows System Sounds found in c:\Windows\Media??
是否有可能System.Media.SoundPlayer无法播放c:\Windows\Media 中的Windows 系统声音?
I have the code:
我有代码:
using (var soundPlayer =
new SoundPlayer(@"c:\Windows\Media\Landscape\Windows Notify.wav"))
{
soundPlayer.Play();
}
Yet when I run this code I get the error:
但是,当我运行此代码时,出现错误:
Sound API only supports playing PCM wave files.
Am I missing something? Is there a way to play these files from a WPF application? (without converting them to PCM)
我错过了什么吗?有没有办法从 WPF 应用程序播放这些文件?(不将它们转换为 PCM)
回答by Sajeetharan
The SystemSounds class contains the following predefined system sounds:
SystemSounds 类包含以下预定义的系统声音:
Asterisk Beep Exclamation Hand Question
Asterisk Beep Exclamation Hand 问题
So for example, to play the Stop:
例如,播放停止:
System.Media.SystemSounds.Hand.Play();
All other sounds require you read the desired sound from the registry and play it with code like this:
所有其他声音都需要您从注册表中读取所需的声音并使用如下代码播放:
SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\Landscape\Windows Notify.wav");
回答by Sheridan
This was an interesting question, but has a simple answer. After reading about this problem, I tried out your code and got the same problem, so then I searched online for a solution. While not finding an exact solution, I did find the SoundPlayer not playing any bundled windows sounds PCM wav filespost here on StackOverflow that showed some code that played an audio file from the Windows\Mediafolder successfully.
这是一个有趣的问题,但有一个简单的答案。在阅读了这个问题后,我尝试了你的代码并遇到了同样的问题,所以我在网上搜索了解决方案。虽然没有找到确切的解决方案,但我确实发现SoundPlayer 没有播放任何捆绑的 Windows 声音 PCM wav 文件在 StackOverflow 上发布,其中显示了一些Windows\Media成功播放文件夹中音频文件的代码。
I tried thatcode and it worked, so then I just had to work out why your example didn't work. I checked for any differences between the audio file that did play and your notify audio file in an audio editor, but they wereboth definitely WAV files.
我尝试了该代码并且它起作用了,所以我只需要弄清楚为什么您的示例不起作用。我检查了音频文件之间的差异是确实发挥和你在音频编辑通知的音频文件,但它们是两种绝对WAV文件。
I tried playing a different audio file from the Windows\Media\Landscapefolder and got the same error. I then tried playing an audio file from a different sub folder in the Windows\Mediafolder and still got the same error. However, I then noticed that many of the folders in the Windows\Mediafolder had the same audio files in.
我尝试从Windows\Media\Landscape文件夹中播放不同的音频文件并得到相同的错误。然后我尝试从文件夹中的不同子文件夹播放音频文件Windows\Media,但仍然出现相同的错误。但是,我随后注意到文件夹中的许多文件Windows\Media夹都有相同的音频文件。
That got me thinking and I eventually realised that allof the audio files that are in these folders are actuallyin the Windows\Mediafolder directly. So you canplay the sounds, but you just have to ignore the ones in the sub folders and play the ones from the Windows\Mediafolder. This will work:
这让我开始思考,我最终意识到这些文件夹中的所有音频文件实际上都Windows\Media直接位于该文件夹中。所以你可以播放声音,但你只需要忽略子文件夹中的声音并播放文件夹中的声音Windows\Media。这将起作用:
SoundPlayer soundPlayer = new SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
soundPlayer.Play();
However, I can't tell you why we got that strange error, but I can only assume that the files in the folder could perhaps be some kind of links to the actual files in the Windows\Mediafolder and simply used by the operating system for grouping them into categories... or something similar.
但是,我不能告诉你为什么我们会得到那个奇怪的错误,但我只能假设文件夹中的文件可能是文件Windows\Media夹中实际文件的某种链接,并且只是由操作系统用于对它们进行分组分类...或类似的东西。
回答by Bret
"Landscape" refers to a Windows "Sound Scheme" and the wav files therein are utilized by the Windows OS. As you have already determined, those files cannot be used directly.
“Landscape”是指 Windows 的“声音方案”,其中的 wav 文件由 Windows 操作系统使用。正如您已经确定的那样,这些文件不能直接使用。
Comparison of the filesize of the Windows Notify.wav within the Landscape directory and the Windows Notify.wav file within the base Windows\Media directory is quite sizable; 222KB vs 25.5KB ---
Landscape 目录中的 Windows Notify.wav 文件大小与基本 Windows\Media 目录中的 Windows Notify.wav 文件的文件大小的比较相当大;222KB 对比 25.5KB ---
If you go into the 'Sound' control panel applet and browse to 'Sounds' tab, you will see a dropdown list for "Sound Scheme:" and those additional folders within Windows\Media will be displayed in that list.
如果您进入“声音”控制面板小程序并浏览到“声音”选项卡,您将看到“声音方案:”下拉列表,Windows\Media 中的其他文件夹将显示在该列表中。
I don't have links to back this up, but after countless hours in dealing with Control Panel Sounds programmatically, I would venture to guess that the 'Sound Scheme' wav files just contain relevant data (perhaps effect data) that is consumed at runtime to play the modified versions of those sounds ... I could be quite wrong about that last part; it's just a guess. Regardless, you won't be able to use those sound scheme files directly within your code.
我没有链接来支持这一点,但是在以编程方式处理控制面板声音无数小时后,我敢猜测“声音方案”wav 文件只包含在运行时消耗的相关数据(可能是效果数据)播放这些声音的修改版本......我可能对最后一部分完全错误;这只是一个猜测。无论如何,您将无法直接在代码中使用这些声音方案文件。
If you must play that "Landscape" version of the Notify wav, then I'd suggest playing the sound and saving it into a new wave file in an audio editor. You can add the wave file to your installer/deployment project to play it into the Windows\Media folder and call it directly the same way that you are already.
如果您必须播放 Notify wav 的“横向”版本,那么我建议您播放声音并将其保存到音频编辑器中的新波形文件中。您可以将 wave 文件添加到您的安装程序/部署项目中,以将其播放到 Windows\Media 文件夹中,并按照您已经使用的方式直接调用它。
回答by Bryce Wagner
Since the sound seems to be in a compressed format, it has to be decompressed before you can play it using SoundPlayer. You can use Windows Audio Compression Manager to decompress the sound for playback:
由于声音似乎是压缩格式,因此必须先将其解压缩,然后才能使用 SoundPlayer 播放。您可以使用 Windows 音频压缩管理器解压缩声音以进行播放:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd742945(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/dd742945(v=vs.85).aspx
However, that's rather complex to implement, so I discovered a nice library that does all that for you, NAudio:
然而,这实现起来相当复杂,所以我发现了一个很好的库,可以为你完成所有这些,NAudio:
Using NAudio seems to be slightly more involved than using System.Media.SoundPlayer, but it also appears to offer far more functionality.
使用 NAudio 似乎比使用 System.Media.SoundPlayer 稍微复杂一些,但它似乎也提供了更多的功能。

