NAudio
NAudio copied to clipboard
NAudio device enumeration stops working when combined with AudioSwitcher
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.
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.
The work around is to use the AudioSwitcher CoreAudioController members/methods instead. GetPlaybackDevices, and DefaultPlaybackDevice, etc.
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.
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.
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.
@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.
@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.