How to play a MP3 file using NAudio

Rookian picture Rookian · Mar 21, 2010 · Viewed 28.1k times · Source
WaveStream waveStream = new Mp3FileReader(mp3FileToPlay);
var waveOut = new WaveOut();
waveOut.Init(waveStream); 
waveOut.Play();

This throws an exception:

WaveBadFormat calling waveOutOpen

The encoding type is "MpegLayer3" as NAudio.

How can I play a mp3 file with NAudio?

Answer

Mark Heath picture Mark Heath · Sep 9, 2013

For users of NAudio 1.6 and above, please do not use the code in the original accepted answer. You don't need to add a WaveFormatConversionStream, or a BlockAlignReductionStream, and you should avoid using WaveOut with function callbacks (WaveOutEvent is preferable if you are not in a WinForms or WPF application). Also, unless you want blocking playback, you would not normally sleep until audio finishes. Just subscribe to WaveOut's PlaybackStopped event.

The following code will work just fine to play an MP3 in NAudio:

var reader = new Mp3FileReader("test.mp3");
var waveOut = new WaveOut(); // or WaveOutEvent()
waveOut.Init(reader); 
waveOut.Play();