|
Hi, I'm trying a simple program, which will record audio, starting and stopping at intervals.
However, in some cases I'm seeing a Windows error message with the following text:
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at NAudio.Wave.WaveIn.Callback(IntPtr waveInHandle, WaveMessage message, IntPtr userData, WaveHeader waveHeader, IntPtr reserved)
at NAudio.Wave.WaveWindow.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
This is happening when I try to start recording, however, the recording actually is done without issues. Sometimes I can stop/restart a couple of times with no problems, but typically once this happens, it pops up again every time I start recording
again.
In my code, I run, at the beginning of the program (WPF, for the record) I do:
waveInStream.WaveFormat = new WaveFormat(16000, 2);
waveInStream.BufferMilliseconds = 1000;
waveInStream.DataAvailable += new EventHandler<WaveInEventArgs>(SendCaptureSamples);
Then, when I want to start recording I do:
RecFileName = getFileName(System.IO.Path.GetTempPath(), "au");
try
{
writer = new WaveFileWriter(RecFileName, waveInStream.WaveFormat);
}
catch (Exception ex)
{
// Log error
writer = null;
}
if (writer != null)
{
try
{
waveInStream.StartRecording();
}
catch (Exception e)
{
// Log error
}
}
My event handler is:
private void SendCaptureSamples(object sender, WaveInEventArgs e)
{
if (writer != null)
{
try
{
writer.Write(e.Buffer, 0, e.BytesRecorded);
}
catch (Exception ex)
{
// Log error
}
}
}
And, when I want to stop recording:
try
{
waveInStream.StopRecording();
writer.Flush();
writer.Close();
writer.Dispose();
}
catch (Exception e)
{
// Log error
}
What bothers me is that the recording happens just fine, but the error dialog is pretty annoying.
I apologize if this seems like a dumb question, but I can't seem to be able to figure out what's going on :(
If anybody can help me fix my code, or, at least set up NAudio so these exceptions get handled silently, it will be greatly appreciated.
|