Receiving “Not a WAVE file - no RIFF header” when

2019-08-24 02:04发布

问题:

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).

回答1:

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));


回答2:

you don't need the second and third lines. Mp3FileReader will convert to PCM for you and you can play it directly with a player like WaveOutEvent. To actually produce a WAV file on disk, pass it into WaveFileWriter.CreateWaveFile

using(var reader = new Mp3FileReader(mp3FileLocation))
{
    WaveFileWriter.CreateWaveFile(reader);
}


标签: c# .net naudio