1

I'm following this tutorial to play MP3 audio files with NAudio. To create the WaveStream I use this method:

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");
            }
            volumeStream = inputStream;
            return volumeStream;
        }

Unfortunately I always get an exception in the

line inputStream = new WaveChanne32(mp3Reader):

Blockquote

System.ApplicationException was unhandled
  Message=Only PCM supported
  Source=NAudio
  StackTrace:

2 Answers 2

2

You can use

var pStream = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(mp3Reader);
var inputStream = new NAudio.Wave.BlockAlignReductionStream(pStream);
0
0

PCM is an encoding type (Pulse-code modulation). Seems like NAudio can only handle PCM encoded files.

3
  • Thanks. And how do I ensure that the file I'm playing is PCM encoded? I encode the MP3 file that I'm trying to play here by myself using Lame Dec 30, 2010 at 21:01
  • I had a look at that example... and I find that very strange. As far as I know a MP3 file is never PCM encoded. So that piece of code cannot work... I think... I'm not a music format specialist, just a programmer who happens to play guitar.
    – Steven K.
    Dec 30, 2010 at 21:13
  • this is not true. NAudio has the MP3FileReader class that can handle MP3 files. The issue is that the WaveChannel32 class takes PCM as an input. i.e. you need to convert MP3 to PCM before passing in to WaveStream32.
    – Mark Heath
    Dec 31, 2010 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.