Search code examples
c#.netnaudio

Receiving "Not a WAVE file - no RIFF header" when creating wave stream from mp3 conversion stream


I'm trying to convert a mp3 into a wave stream using NAudio. Unfortunately I receive the error Not a WAVE file - no RIFF header on the third line when creating the wave reader.

var mp3Reader = new Mp3FileReader(mp3FileLocation);
var pcmStream = WaveFormatConversionStream.CreatePcmStream(mp3Reader);
var waveReader = new WaveFileReader(pcmStream)

Shouldn't these streams work together properly? My goal is to combine several mp3s and wavs into a single stream for both playing and saving to disc( as a wav).


Solution

  • I'm going to preface this with a note that I've never used NAudio. Having said that, there's a guide to concatenating audio on their Github site.

    Having looked at the API, you can't use Mp3FileReader directly as it doesn't implement ISampleProvider. However, you can use AudioFileReader instead.

    Assuming you have an IEnumerable<string> (aka List or array) of the filenames you want to join named files:

    var sampleList = new List<ISampleProvider>();
    
    foreach(string file in files)
    {
        sampleList.add(new AudioFileReader(file));
    }
    
    WaveFileWriter.CreateWaveFile16("outfilenamegoeshere.wav", new ConcatenatingSampleProvider(sampleList));