Playing a .WAV file in .NET

我的梦境 提交于 2019-12-29 04:50:20

问题


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

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

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


回答1:


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



回答2:


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

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

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

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




回答3:


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

because sound play asynchronically.




回答4:


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

To run, open in linqpad, set the value of wavFilePath to a local wave file path, and hit F5. Play is async, so we do a Console.ReadLine to wait until it's done.

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:



来源:https://stackoverflow.com/questions/1284322/playing-a-wav-file-in-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!