Got error "Not a WAVE file - no RIFF header"

2.8k Views Asked by At

This is how I speak "Hello" using SpeechSynthesizer , convert with NAudio WaveFormatConversionStream and read it again .

And I got the error

Not a WAVE file - no RIFF header

 int count = sourceFiles.Count;   
    WaveFileReader[] reader = new WaveFileReader[count];    

   var _wavStream = new MemoryStream();
                            using (SpeechSynthesizer synth = new SpeechSynthesizer())
                            {
                                synth.SetOutputToWaveStream(_wavStream);
                                synth.Speak("Hello");
                                _wavStream.Position = 0;
                                int outRate = 44100;
                                var outFormat = new WaveFormat(outRate, 16, 1);
                                using (var resampler = new WaveFormatConversionStream(outFormat, new WaveFileReader(_wavStream)))
                                {
                          reader[i] = new WaveFileReader(resampler); <====GotTheErrorHere 

How can I fix it ?
Thanks !

1

There are 1 best solutions below

1
On

The speech synthesizer has not made a WAV file, so there is no RIFF header. instead of WaveFileReader you should be using RawSourceWaveStream and passing in the memory stream and the correct WaveFormat that the speech synthesizer is outputting .