Reputation: 27
How to detect when an audio device has been plugged and unplugged using C#?
Upvotes: 0
Views: 1875
Reputation: 108975
Your best approach is probably WMI. You can subscribe to WMI events reflecting the creation/destruction of instances of most WMI classes. An instance of Win32_SoundDevice
exists for each audio device.
If you want details of the status of a specific device (on speakers, muted, ...) then WMI doesn't appear to include this.
YOu'll probably need to use COM interop to access the MMDevice API which includes the ability to enumerate "endpoint" devices. This will be hard work as there doesn't appear to be a typelib you can import, rather you'll need to either write some C/C++ to provide a proxy or re-create the necessary COM interfaces etc. in .NET manually.
Upvotes: 1