This project has moved. For the latest updates, please go here.

Problem with WaveFileReader(inputStream)

Dec 24, 2009 at 1:18 PM
Edited Dec 24, 2009 at 1:19 PM


I have a problem with WaveFileReader. I'm trying to pass MemoryStream with recorded audio to WaveFileReader:

waveStream = new WaveFileReader(inputStream);

And on this line I have Exception: The non-negative number is required. Parametre name: value. Can you tell me, where I was mistaken?

Jan 8, 2010 at 2:40 PM

WaveFileReader expects a WAV file header, not just recorded audio. Does your memory stream contain a correctly formatted WAV header?

Jan 8, 2010 at 4:03 PM
Edited Jan 8, 2010 at 4:07 PM

I so think, because at creation of object WaveFileWriter I set MemoryStream as argument.

var writer = new WaveFileWriter(inputStream, recordingFormat);

It means that WaveFileWriter writes down a data from a microphone in MemoryStream. And after recording sound I try to read data from MemoryStream:

 var waveStream = new WaveFileReader(inputStream);

As I have written earlier, here I have an exception.

If I was mistaken, and MemoryStream does not contain  WAV header, what you can suggest me to solve my problem, where and how I can get WAV header? And if I write down a sound in a file, no exceptions arise. What distinction of writing in a file and in a stream?

Jan 8, 2010 at 5:58 PM

do you set the inputStream position back to 0 before you call WaveFileReader?

Jan 9, 2010 at 6:11 AM
Of cource yes,
inputStream.Seek(0, SeekOrigin.Begin);
Anyway, I can not understand defference between writing in a file and writing in a stream.
Jan 11, 2010 at 9:10 AM

I have written applications that do exactly what you are trying to do myself, so I'm not sure what exactly is going wrong.

Are you able to discover which line of code within the NAudio source code you are getting the exception from? (does the stack trace go any deeper?)



Aug 18, 2010 at 2:04 PM

Same thing here, I've recorded audio and put it in a MemoryStream 

                    waveInStream = new WaveIn();
                    ms = new MemoryStream();
                    writer = new WaveFileWriter(ms, waveInStream.WaveFormat);

                    waveInStream.DataAvailable += new EventHandler<WaveInEventArgs>(waveInStream_DataAvailable);
                    btnGravar.Text = "STOP";
                    btnGravar.BackColor = Color.Gray;
                catch (Exception ex)
                    MessageBox.Show(ex.Message, "Erro!",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);

Later, after the user stops the record.


            if (waveInStream != null)

                btnGravar.Text = "REC";
                btnGravar.BackColor = Color.Red;



after, when the user selects where to save the file (.wav)


                FileStream fs = null;
                    ms.Position = 0;
                    fs = new FileStream(sfdSalvarArquivo.FileName, FileMode.Create, FileAccess.Write);
                catch (Exception ex)
                    throw ex;
                    if (fs != null)
                    if (waveInStream != null)
                        waveInStream = null;
                    if (writer != null)
                        writer = null;


The Stream is wrote in the file but I can't reproduce it.


What I'm mistaking ?