question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Crash in WasapiCapture when device is unplugged

See original GitHub issue

I have implemented something similar to “How to record and play audio at the same time with NAudio”, but using a USB audio device, WasapiCapture and IWaveProvider. Everything works correctly until I unplug the device.

When the device is unplugged, an exception seems to be thrown in the capture thread of WasapiCapture, here’s the stack trace :

System.IO.FileNotFoundException
  HResult=0x80070015
  Message=Non implémenté

Non implémenté

  Source=NAudio.Wasapi
  StackTrace:
   at NAudio.CoreAudioApi.Interfaces.IAudioClient.Stop()
   at NAudio.CoreAudioApi.AudioClient.Stop()
   at NAudio.CoreAudioApi.WasapiCapture.CaptureThread(AudioClient client)
   at NAudio.CoreAudioApi.WasapiCapture.<StartRecording>b__35_0()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

(Non implémenté means Not implemented)

It looks like the call to client.stop() in the finally block of CaptureThread fails, and I cannot catch the exception.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
JacqiPancommented, Apr 30, 2021

After some debugging, I don’t think it has to do with the client being null. If I make something like this :

Since AudioClient is a COM object, it simply loses its reference as soon as it’s unplugged. So it’s not technically null, as stated above (“null”), and will always throw an exception when you try to access it after the endpoint device disconnects,. The try-catch solution works, since it catches the exception for AudioClient just like it did within DoRecording()

I could do some dubious things to get the audioClient private variable and check if it’s null, but I don’t think it’s a good solution.

It is already available within the MMDevice class, which can be accessed through a MMDeviceEnumerator. If you use WasapiCapture without specifying an MMDevice, it will just use your Windows default audio device MMDeviceEnumerator.GetDefaultAudioEndpoint(DataFlow dataFlow, Role role)

I used something like this:

 private void OnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
    {
            try
            {
                if (CaptureMMDevice.AudioClient != null) // Will throw as soon as the device is unplugged
                    bufferedWaveProvider.AddSamples(waveInEventArgs.Buffer,0, waveInEventArgs.BytesRecorded);
            }
	    catch
            {		
                WasapiCaptureClient.Stop();
                WasapiCaptureClient -= OnDataAvailable;			
	    }		
    }
0reactions
loics2commented, May 3, 2021

Oh I wasn’t aware the audio client was accessible from the MMDevice, and I store it in a variable somewhere. It works correctly, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

WASAPI device very frequently crashing on ...
The crash occurs when releasing the sessionEventCallback. The IAudioSessionEvents pointer is a dead pointer here so I suspect that there is some ...
Read more >
Manage Crash Detection on iPhone 14 models
What is Crash Detection? If your iPhone 14 detects a severe car crash, it can help connect you to emergency services and notify...
Read more >
Use Crash Detection on iPhone or Apple Watch to call for ...
If your iPhone or Apple Watch detects a severe car crash, your device can help connect you to emergency services.
Read more >
Crash of Cars - Apps on Google Play
Welcome to Crash of Cars, a REAL-TIME MULTIPLAYER game where your goal is to collect as many crowns as possible before getting destroyed....
Read more >
GEORGIA MOTOR VEHICLE CRASH REPORT OVERLAY
GEORGIA MOTOR VEHICLE CRASH REPORT. OVERLAY ... 9-Backup Due to Prior Crash/Secondary Crash. 9-Panel Truck ... 30-Talking on Hands-Free Device (Distracted).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found