检测是否插入耳机,或不通过C#堵塞 [英] Detect if headphones are plugged in or not via C#

查看:481
本文介绍了检测是否插入耳机,或不通过C#堵塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有比如如何检测是否插入耳机,或不通过C#堵塞。

There is no example how to detect if headphones are plugged in or not via C#.

我想应该是一些事件......

I assume should be some event for that...

有一定道理使用WMI?

Does make sense to use WMI?

 ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\cimv2",
                                                                  "SELECT * FROM Win32_SoundDevice");

foreach (ManagementObject queryObj in searcher.Get())
{
    Console.WriteLine("-----------------------------------");
    Console.WriteLine("Win32_SoundDevice instance");
    Console.WriteLine("-----------------------------------");
    Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]);
}



会有人如此高兴规定的?

Would anyone be so pleased to provide it?

感谢您!

推荐答案

检测这种变化是可以通过使用的 IMMDeviceEnumerator :: RegisterEndpointNotificationCallback 方法。

Detecting changes of this kind is possible by using the IMMDeviceEnumerator::RegisterEndpointNotificationCallback method.

当你要处理这在C#中,您将需要它已经被阿科什Mattiassich实现的托管包装。你可以在这里找到一个thourough例如:托管包装周围MMAudioDeviceApi

As you want to handle this in C# you will need a managed wrapper which has already been implemented by Akos Mattiassich. You can find a thourough example here: Managed Wrapper around MMAudioDeviceApi

他说:

该程序可以在选定的设备和它玩测试声音自动更新上的变化如表。通过控制面板或在物理上插入新的设备的情况下。

The program can play test sound on selected devices and it updates the list automatically on changes eg. through the control panel or in case of plugging new device physically.

这篇关于检测是否插入耳机,或不通过C#堵塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆