Need help with exception thrown by NAudio

Oct 27, 2012 at 5:20 AM

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.

Coordinator
Oct 27, 2012 at 7:52 AM

what version of NAudio are you using?

Oct 27, 2012 at 11:48 PM
Edited Oct 28, 2012 at 2:49 AM

I use NuGet.  First tries were with v1.5, but now I've updated to v1.6.

Just to be clear, my program doesn't crash, and the recordings work fine, but I get this annoying dialog, which I'd want to get rid of.

Also, in case it's relevant, this is in Windows 7 (Ultimate and Home Premium), I'm using VS 2012 and I'm targetting .NET v4.0 (Client Profile). 

Thanks for any help.

Coordinator
Nov 3, 2012 at 5:09 PM

not sure what is causing this, I'll try to reproduce if possible. It is possibly related to http://naudio.codeplex.com/workitem/16347

Nov 17, 2012 at 1:12 AM
Edited Nov 17, 2012 at 4:30 AM

Thanks,  it doesn't seem like there has been any progress in the bug report you listed. 

 Is there anything I can do to help? What I'm working on is a simple tool to help friends and family and while not critical, it is pretty annoying to have that Windows diag pop up every now and then.

Since nothing seems to be working, I imagine I could simply either check for null, or reset something to get rid of this?

Coordinator
Nov 17, 2012 at 7:31 AM

Yes, you could do a null check, although I can't understand what is causing it to be null. What callback model are you using.

Alternatively, you could try using WaveInEvent instead, which may be more reliable.

Mark

Nov 20, 2012 at 3:04 PM

Well, I really put all the NAudio-related code in my original post.  I admit I simply tried to follow the tutorials listed here (which point to a blog).  Not sure what "callback model" I use, but, the code is:

waveInStream.DataAvailable += new EventHandler<WaveInEventArgs>(SendCaptureSamples);

So sounds like I should look into WaveInEvent then, instead of DataAvailable.  I certainly can try that, do you have a pointer to either an example or any other doc I can check?

Coordinator
Nov 20, 2012 at 3:07 PM

No, your original post is missing where where you create waveInStream. Somewhere in your code there should be a line that starts with waveInStream =

what is on the other side of the equals statement?

try changing it to waveInStream = new WaveInEvent();

Nov 20, 2012 at 3:58 PM

Ah, of course, you're right.  I had that in the declarations:

        WaveIn waveInStream  = new WaveIn();
I can change that, is there anything else I'd need to change? Again, if this is already documented somewhere else, just point me there.

Thanks for the help!

Coordinator
Nov 20, 2012 at 4:01 PM

No, just change that and see if it helps. Only thing to note is that now the DataAvailable event will be coming in on a background thread, so don't try to write to the GUI without doing a BeginInvoke on your form.

Nov 27, 2012 at 11:56 PM

Ok, initial tests with the suggested change seem to indeed have solved the problem.

I'll keep testing, but it's very promising!

Thanks for the help and keep up with the good work!