tags:

views:

294

answers:

1
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?

+1  A: 

Try like this:

class Program
{
    static void Main()
    {
        using (var ms = File.OpenRead("test.mp3"))
        using (var rdr = new Mp3FileReader(ms))
        using (var wavStream = WaveFormatConversionStream.CreatePcmStream(rdr))
        using (var baStream = new BlockAlignReductionStream(wavStream))
        using (var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
        {
            waveOut.Init(baStream);
            waveOut.Play();
            while (waveOut.PlaybackState == PlaybackState.Playing)
            {
               Thread.Sleep(100);
            }
        }
    }
}
Darin Dimitrov
This does not work. "InvalidParameter calling acmStreamPrepareHeader"
Rookian
I've just downloaded the latest version of NAudio (http://www.codeplex.com/naudio) and tested this code with an mp3 on my computer. It worked for me.
Darin Dimitrov
Do you have a 64bit environment? I have recompiled the solution using x64, release mode and changed the .net platform from .NET 2.0 to 3.5 ... maybe because of this I get the errors?
Rookian
Yes I am running on Win7 64bit, I had to target x86 in the project properties for this to work. Targeting `Any CPU` threw a `BadImageFormatException` for `NAudio.dll`.
Darin Dimitrov
thanks! Now it works :) I read anywhere that one shall recompile NAudio to x64 ...
Rookian
x64 support in NAudio is hopefully coming very soon. See recent checkins - http://naudio.codeplex.com/SourceControl/list/changesets
Mark Heath