NAudio, .WAV files being created but not supported upon playback.

I can press record, record some audio then press stop – a save file dialog opens up and I can save that recording in .WAV format for future playback. Only when I open up the file for playback it doesn't work. Spoke to Mark Heath (NAudio creator and main contributor), and he recommended I call Dispose. It hasn't done anything but I think I know the issue.

private void sourceStream_DataAvailable(object sender, WaveInEventArgs e) { if (waveWriter == null) return; //Adds bytes to the wave file, storing them in a buffer? waveWriter.Write(e.Buffer, 0, e.BytesRecorded); waveWriter.Flush(); } private void recordButton_Click(object sender, RoutedEventArgs e) { MainWindow mw = new MainWindow(); //Get device number from MainWindow.DeviceButton(). int devNum = mw.DeviceButton(); if (recordButton.Content.ToString() == "RECORD") { recordButton.Content = "STOP"; //Start recording audio. sourceStream = new WaveIn(); sourceStream.DeviceNumber = devNum; //1. Set the sample rate(?). 2. Get number of channels supported on the device. sourceStream.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(devNum).Channels); sourceStream.StartRecording(); sourceStream.DataAvailable += new EventHandler<WaveInEventArgs>(sourceStream_DataAvailable); } else if(recordButton.Content.ToString() == "STOP") { sourceStream.StopRecording(); save.Filter = "Wave Files (*.wav)|*.wav;"; if (save.ShowDialog() != true) return; waveWriter = new WaveFileWriter(save.FileName, sourceStream.WaveFormat); waveWriter.Dispose(); recordButton.Content = "RECORD"; } } 

I think that waveWriter should be instantiated after StartRecording() is called. It's description is that it writes .WAV data to a .WAV file on disk so this could make sense. Trying this however produces a runtime error of type 'ArgumentException'. This is quite a general exception and so I'm not exactly sure why I can't move my WaveFileWriter instance to the first if() statement.

Sorry for the code and text dump. Any and all help appreciated.

by HUMBLEFART via /r/csharp

Leave a Reply