MMDevice can only be used on the thread they were created on.
See original GitHub issueThere seems to be an issue with using MMDevice in a multithreaded scenario.
main thread:
var device = (MMDevice) comboWasapiDevices.SelectedItem;
background processing thread:
IWaveIn newWaveIn;
Task task = new Task(() => {
newWaveIn = new WasapiCapture(device) // <----------------- THROWS EXCEPTION
});
task.start();
Exception Thrown Message: An exception of type ‘System.InvalidCastException’ occurred in NAudio.dll but was not handled in user code
{“Unable to cast COM object of type ‘System.__ComObject’ to interface type ‘NAudio.CoreAudioApi.Interfaces.IMMDevice’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{D666063F-1587-4E43-81F1-B948E807363F}’ failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).”}
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
COM Issue · Issue #425 · naudio/NAudio
I have a class that holds the MMDevice. If that class is called from an event being raised, the device (which is a...
Read more >vlc.py returns ""cannot initialize COM" - The VideoLAN Forums
I've written a PyQt4 app that uses VLC. I've primarily developed it on a Linux machine but it's intended for use on Win7-64...
Read more >Realm objects can only be accessed on the thread they ...
The problem is that your Realm object is created only once on first call to RealmProcessor.with (because it is singleton).Lets say that ...
Read more >158099 - Races inside the MMDevice API with ...
Looks like a bug in MMDevApi.dll to me. We're constructing and destructing the object from the same thread, but from inside RegisterEndpointNotificationCallback ...
Read more >IAudioClient::Initialize (audioclient.h) - Win32 apps
Calls from an MTA thread may result in undefined behavior. An attempt to create a shared-mode stream can succeed only if the audio...
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
I think the code for that is in the WPF demo app. It’s not made it into the main NAudio library, but you can easily copy the code and adapt it to the number of bands you want, and frequencies you would like
IMO “every” object created by a thread is owned by it. So if you really want to use these devices within a thread (here Task) easily you have to create it within. Yes, there are other ways to do it, but I want to keep it simple here.
The following code do select the device by index given to the task:
this.DevColIn is the device list created by UI thread. Hope this helps.