Play a .wav file in .NET.

I am trying to write a SAMPLER program where each key has a different sound (wav file).

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

If that matters, I work with Microsoft Visual C # and using WinForms.

+6
c # file wav
source share
4 answers
SoundPlayer simpleSound = new SoundPlayer(strAudioFilePath); simpleSound.Play(); 
+21
source share

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

fortunately, they seem to provide a C # shell for the best audio API you could imagine, and you don’t have to change one line of code so your code runs on a playstation or xbox or something else developers pretty much react (you report an error in the evening, go to bed, and the corrected assembly is available when you wake up) the documentation is readable, understandable and a HUGE many examples in the SDK, which makes useless providing the tutorial, since the documentation is pretty much perfect.

wav playback with FMOD - only 5 lines of code, and only 4 lines you can apply effects when linking the balance and volume of playback to the 3d engine (to handle intersections between the point of consc and the sound source, 4 lines ....

if you want (use C # for) to make a sound, β†’ FMOD.

+4
source share
 SoundPlayer simpleSound = new SoundPlayer(strAudioFilePath); simpleSound.PlaySync(); 

because the sound is played asynchronously.

+1
source share

This console solution uses LINQPad (so the calls to the .Dump () extension method) and NAudio (you'll notice that I use the full namespace on several classes to clarify). To tune in correctly, you can simply download the snippet from http://share.linqpad.net/d7tli8.linq (I added NAudio from NuGet).

To start, open in linqpad, set the wavFilePath value to the path of the local wave file and press F5. Play is asynchronous, so we do Console.ReadLine to wait until it ends.

 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 of all .Dump calls looks on my machine, if you're interested:

audiofilereader contents

sampleprovider and waveout info

0
source share

All Articles