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.

MMDevice can only be used on the thread they were created on.

See original GitHub issue

There 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:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
markheathcommented, May 17, 2019

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

1reaction
kruggcommented, Jul 26, 2017

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:

            WasapiCapture device = null;
            Task task = Task.Factory.StartNew((object devIndex) =>
            {
                var index = (int)devIndex;

                var deviceEnum = new MMDeviceEnumerator();
                var threadDeviceList = deviceEnum.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active).ToList();

                var threadDevice = threadDeviceList[index];
                device = new WasapiCapture(threadDevice); // <----------------- SHOULD NOT THROW EXCEPTION
            }, this.DevColIn.IndexOf(this.SelectedInputDevice));
            //task.Start();

this.DevColIn is the device list created by UI thread. Hope this helps.

Read more comments on GitHub >

github_iconTop 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 >

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