C# 在 .NET 中播放 .WAV 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1284322/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:39:56  来源:igfitidea点击:

Playing a .WAV file in .NET

c#.netfilewav

提问by

I'm trying to write a SAMPLER program, where each key has a different sound (a WAV file).

我正在尝试编写一个 SAMPLER 程序,其中每个键都有不同的声音(WAV 文件)。

Can someone explain to me or give me a link to an explanation where i can learn how to play the WAV files?

有人可以向我解释或给我一个解释的链接,我可以在那里学习如何播放 WAV 文件?

If it matters, I'm working with Microsoft Visual C# and using WinForms.

如果重要的话,我正在使用 Microsoft Visual C# 并使用 WinForms。

回答by Arsen Mkrtchyan

SoundPlayer simpleSound = new SoundPlayer(strAudioFilePath);
simpleSound.Play();

回答by user217299

use fmod, which is simply the best sound library in the whole universe

使用fmod,简直就是全宇宙最好的声音库

fortunately, they seem to provide a C# wrapper for the best audio API you could try to imagine, and you won't have to change a single line of code to make your code working on playstation or xbox or whatever the developers are pretty much reactive (you report a bug in the evening, go to bed, and the corrected build is available as you wake up) documentation is readable, understandable, and HUGE lot of examples in the SDK, which makes it useless to provide a tutorial since documentation is pretty much perfect

幸运的是,它们似乎为您可以尝试想象的最佳音频 API 提供了一个 C# 包装器,并且您无需更改一行代码即可使您的代码在 playstation 或 xbox 或任何开发人员非常敏感的东西上工作(您在晚上报告错误,上床睡觉,并且在您醒来时可以使用更正的构建)文档可读、易于理解,并且 SDK 中有大量示例,这使得提供教程毫无用处,因为文档是非常完美

playing a wav with FMOD is just 5 lines of code, and with just 4 lines more you can apply effects while linking the balance and volume of the playback to a 3d engine (to handle intersections between the consc point and the audio source, 4 lines....

使用 FMOD 播放 wav 只需 5 行代码,只需 4 行代码,您就可以在将播放的平衡和音量链接到 3d 引擎时应用效果(处理 consc 点和音频源之间的交叉点,4 行....

if you want to (use C# to) do sound, -> FMOD.

如果你想(使用 C# 来)做声音,-> FMOD。

回答by Arman

SoundPlayer simpleSound = new SoundPlayer(strAudioFilePath);
simpleSound.PlaySync();

because sound play asynchronically.

因为声音是异步播放的。

回答by Pat

This console-based solution uses LINQPad (thus the .Dump() extension method calls) and NAudio(you'll notice that I use the full namespace on a couple of classes just to clarify). To get set up correctly, you can just download the snippet from http://share.linqpad.net/d7tli8.linq(I added NAudio from NuGet).

这个基于控制台的解决方案使用 LINQPad(因此使用 .Dump() 扩展方法调用)和NAudio(您会注意到我在几个类上使用完整的命名空间只是为了澄清)。要正确设置,您只需从http://share.linqpad.net/d7tli8.linq下载代码片段(我从 NuGet 添加了 NAudio)。

To run, open in linqpad, set the value of wavFilePathto a local wave file path, and hit F5. Playis async, so we do a Console.ReadLineto wait until it's done.

要运行,请在 linqpad 中打开,将 的值设置wavFilePath为本地波形文件路径,然后按 F5。Play是异步的,所以我们做一个Console.ReadLine等待它完成。

string wavFilePath = @"TODO";
var reader = new NAudio.Wave.AudioFileReader(wavFilePath);
reader.Dump("AudioFileReader");
var sampleProvider = reader.ToSampleProvider().Dump("sample provider");

NAudio.Wave.WaveOut.DeviceCount.Dump("num waveout on comp");
var outputDeviceInfo = WaveOut.GetCapabilities(0).Dump();
var outputter = new WaveOut() {
    DesiredLatency = 5000 //arbitrary but <1k is choppy and >1e5 errors
    , NumberOfBuffers = 1 // 1,2,4 all work...
    , DeviceNumber = 0
}.Dump();
outputter.Init(reader);
outputter.Play(); // async
Console.Read();
outputter.Stop();

And this is what the output from all the .Dump calls looks like on my machine, in case you're wondering:

这就是所有 .Dump 调用的输出在我的机器上的样子,以防你想知道:

audiofilereader contents

音频文件阅读器内容

sampleprovider and waveout info

sampleprovider 和 waveout 信息