NAudio icon indicating copy to clipboard operation
NAudio copied to clipboard

NAudio device enumeration stops working when combined with AudioSwitcher

Open MortInfinite opened this issue 6 years ago • 7 comments

With NAudio commit 420e607 or NAudio NuGet version 1.8.5, I receive the following exception, when trying to create a new instance of MMDeviceEnumerator.

System.InvalidCastException: 'Unable to cast object of type 'System.__ComObject' to type 'NAudio.CoreAudioApi.Interfaces.MMDeviceEnumeratorComObject'.'

The problem occurs if calling the following code:

var coreAudioController = new AudioSwitcher.AudioApi.CoreAudio.CoreAudioController();
var deviceEnumerator = new NAudio.CoreAudioApi.MMDeviceEnumerator();

The problem does not occur if calling the following code:

var deviceEnumerator = new NAudio.CoreAudioApi.MMDeviceEnumerator();

It appears that AudioSwitcher interferes with the creation of the NAudio.CoreAudioApi.Interfaces.MMDeviceEnumeratorComObject object that NAudio uses for device enumeration.

Since this problem only occurs when using both AudioSwitcher (Version 3.0.0) and NAudio, I don't know if this is the correct place to report the problem or if I should post it in the AudioSwitcher project.

MortInfinite avatar Dec 12 '18 11:12 MortInfinite

Hmm, not sure why that might be happening. Feel free to ask over at AudioSwitcher to see if they can explain what's going on.

markheath avatar Dec 22 '18 10:12 markheath

The work around is to use the AudioSwitcher CoreAudioController members/methods instead. GetPlaybackDevices, and DefaultPlaybackDevice, etc.

XySoftNeil avatar Jan 17 '20 16:01 XySoftNeil

Same problem here. Tried to use another library CSCore instead of NAudio, the same problem shows. So I assume the problem is AudioSwitcher. I need AudioSwitcher to set the default devices. And the NAudio to do the detail settings, thought it would work. Bad luck.

MultiverseBoy avatar Aug 18 '20 22:08 MultiverseBoy

Same problem but i solved it. The AudioSwitcher.AudioApi.CoreAudio has conflict with NAudio.CoreAudioApi, you need to switch the order. set the AudioSwitcher.AudioApi.CoreAudio under the NAudio.CoreAudioApi.

snippyy avatar Mar 09 '22 18:03 snippyy

Same problem but i solved it. The AudioSwitcher.AudioApi.CoreAudio has conflict with NAudio.CoreAudioApi, you need to switch the order. set the AudioSwitcher.AudioApi.CoreAudio under the NAudio.CoreAudioApi.

I stopped using NAudio, so I can't check if the workaround solves the problem for me, but I hope your suggestion can help others.

MortInfinite avatar Mar 10 '22 16:03 MortInfinite

@snippyy Same problem here, but what exactly do you mean by "set the AudioSwitcher.AudioApi.CoreAudio under the NAudio.CoreAudioApi."

Can you give me a hint please.

KWB avatar May 05 '23 10:05 KWB

@snippyy Same problem here, but what exactly do you mean by "set the AudioSwitcher.AudioApi.CoreAudio under the NAudio.CoreAudioApi."

Can you give me a hint please.

Make sure you are initializing MMDeviceEnumerator before initializing CoreAudioController. Just had this issue and switching the order solved it. I however have not tested the application fully to determine if it will run into other issues with both trying to access COM.

TYLOGIC avatar Sep 15 '23 21:09 TYLOGIC