Pergunta

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:
Foi útil?

Solução

You can use

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

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top