Can not play .mp3 file.

Feb 11, 2011 at 3:22 PM

Good day,

I follow the instruction http://naudio.codeplex.com/wikipage?title=MP3&ProjectName=naudio to play a .mp3 file, but always get exception on line "throw new ArgumentException("Not a supported encoding");" in public void SetWaveFormat(WaveFormat waveFormat) of DmoMediaType.cs  I tired different .mp3 files, all gave me same error.

waveFormat = "NAudio.Wave.WaveFormatExtensible] = {32 bit PCM: 48kHz 2 channels wBitsPerSample:32 dwChannelMask:3 subFormat:00000003-0000-0010-8000-00aa00389b71 extraSize:22}"

        waveFormat.Encoding    Extensible    NAudio.Wave.WaveFormatEncoding

-        waveFormat    {32 bit PCM: 48kHz 2 channels wBitsPerSample:32 dwChannelMask:3 subFormat:00000003-0000-0010-8000-00aa00389b71 extraSize:22}    NAudio.Wave.WaveFormat {NAudio.Wave.WaveFormatExtensible}
+        [NAudio.Wave.WaveFormatExtensible]    {32 bit PCM: 48kHz 2 channels wBitsPerSample:32 dwChannelMask:3 subFormat:00000003-0000-0010-8000-00aa00389b71 extraSize:22}    NAudio.Wave.WaveFormatExtensible
        averageBytesPerSecond    384000    int
        AverageBytesPerSecond    384000    int
        BitsPerSample    32    int
        bitsPerSample    32    short
        BlockAlign    8    int
        blockAlign    8    short
        Channels    2    int
        channels    2    short
        Encoding    Extensible    NAudio.Wave.WaveFormatEncoding
        ExtraSize    22    int
        extraSize    22    short
        sampleRate    48000    int
        SampleRate    48000    int
        waveFormatTag    Extensible    NAudio.Wave.WaveFormatEncoding

Here are my codes:

 

private void button1_Click(object sender, EventArgs e)
        {
            //Declarations required for audio out and the MP3 stream

            waveOutDevice = new WasapiOut(AudioClientShareMode.Shared, 100);
            mainOutputStream = CreateInputStream("Lily.mp3");
            waveOutDevice.Init(mainOutputStream);
            waveOutDevice.Play();
        }

        private WaveStream CreateInputStream(string fileName)
        {
            WaveChannel32 inputStream;
            if (fileName.EndsWith(".mp3"))
            {
                WaveStream mp3Reader = new Mp3FileReader(fileName);
                inputStream = new WaveChannel32(mp3Reader);
            }
            else
            {
                throw new InvalidOperationException("Unsupported extension");
            }
            return inputStream;
        }

I spend many time on it, but just could not play, I am using VS 2008 EXPRESS Edition.

Can you double check if anything missing in your example? Could you put an example of playback a .mp3 file in NAduioDemo project?

Thanks,

Hanson

Coordinator
Feb 11, 2011 at 3:24 PM

there already is an example of MP3 playback in NAudio Demo, just load up an MP3 file and press play

Mark

Feb 11, 2011 at 11:05 PM

Thank you, your NAduioDemo works. I figured it out. The PC I am using does not support WASAPI.