Crash in WasapiCapture when device is unplugged
See original GitHub issueI 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:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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()
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:
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!