This project has moved. For the latest updates, please go here.

Best way to play audio file

Dec 28, 2011 at 8:10 AM

Okay, I know this sounds like a very easy question to some but I am really stuck here. Indeed, I am building an audio player using Naudio and I have realized that in many tutorials people always show easy ways to get you started. However, in my opinion, they always forget to show how things are actually done in a real application. For example, when playing music with Naudio, I would do something like: 

Void PlayAudioMusic(string FilePath)


 using (var ms = File.OpenRead(FilePath))
(var rdr = new Mp3FileReader(ms))
(var wavStream = WaveFormatConversionStream.CreatePcmStream(rdr))
(var baStream = new BlockAlignReductionStream(wavStream))
(var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))



This is great for testing in a simple console application. This however isn't useful if you're actually building a serious application. For example, what many tutorials never say is for example how to handle the most critical things such as:

  1. Disposing resource and when to do it
  2. The best ways to handle different exceptions
  3. What to do before you pause, stop, rewind or even exit the application
  4. Other stuffs I don't even know exist.

Since I am going through this process and have notice that my application has way too many exceptions thrown, can someone please share like a wrapper class around Naudio that he used to handle this. I am sure this will answer many of the trouble some of us have been going through when trying to use Naudio.


Dec 28, 2011 at 8:16 AM

First, I don't know where you got the code sample you show above, but it is not a good way to use NAudio. waveOut.Play just starts playback, and waveOut will get disposed before playback is complete.

The best place to see how to use NAudio in a real application is the NAudioDemo application source code that is part of NAudio. It has lots of examples of how to use NAudio for playback.


Dec 28, 2011 at 8:26 AM

Thanks for your quick reply. I got this sample code from a stackoverflow thread.


Anyways, I have downloaded the Naudio sample code and honestly, I do not understand how it works. It contains way to many different files that I was unable to figure out which one does the audio playing. 

Dec 28, 2011 at 8:44 AM

OK, the code at SO includes a Thread.Sleep in a loop, without which it won't work.

Have a look at AudioPlaybackPanel.cs in the NAudioDemo project.

Dec 28, 2011 at 8:47 AM

Thanks a lot. I'm gonna take a look at it.