Detect device change/disconnect
Nicks182 opened this issue · 4 comments
Hi
I'm probably just being an idiot, but I can't see a way to detect when a device is disconnected or changed.
For example, I would like to be able to get an event/notification when I turn on my Bluetooth headset. Normally I use my USB headset, but when I turn on my BT set then Windows will automatically switch to the BT set. I would like to be able to detect when the USB set is disconnected so I can react to it.
From what I can see on learn.microsoft.com, it should be possible with device events, but I can't see how to use these in this library.
https://learn.microsoft.com/en-us/windows/win32/coreaudio/device-events
Unfortunately, the IMMNotificationClient interface is not properly implemented into the library... but it doesn't look too hard. Let me see if I can get it done by the end of the day.
Please update your version of CoreAudio to use the IMMNotificationClient interface.
You will now see the exposed event handlers to handle notifications:
var devEnum = new MMDeviceEnumerator();
var client = new MMNotificationClient(devEnum);
client.DeviceAdded += (object o, DeviceNotificationEventArgs e) => {
// Handle added device...
};
Thank you so much! Really appreciate it.
Hi, I am attempting to detect devices connection and disconnection. I tried what @morphx666 suggested, but it did not trigger whenever a new device was plugged in. As a workarround I was make it work using the following code:
var devEnum = new MMDeviceEnumerator();
var client = new MMNotificationClient(devEnum);
client.DeviceStateChanged += OnOutputDeviceChange;
private void OnOutputDeviceChange(object o, DeviceStateChangedEventArgs e)
{
if (e.DeviceState == DeviceState.Active) { Debug.WriteLine("Device is active"); }
if (e.DeviceState == DeviceState.Disabled) { Debug.WriteLine("Device is disabled"); }
if (e.DeviceState == DeviceState.NotPresent) { Debug.WriteLine("Device is not present"); }
if (e.DeviceState == DeviceState.Unplugged) { Debug.WriteLine("Device is unplugged"); }
}