简体   繁体   English

连接或断开新的USB音频设备时,是否在C#应用程序中接收通知?

[英]Receive notification in C# app when a new USB audio device is connected or disconnected?

I have a C# Windows Forms app that runs on Windows 8.1 or newer and does speech recognition. 我有一个在Windows 8.1或更高版本上运行的C#Windows Forms应用程序,并且可以进行语音识别。 I want to be notified when a new USB audio input device is connected to the system. 当新的USB音频输入设备连接到系统时,我想收到通知。 I am hoping there is a notification service in the Windows API that will tell me when and audio device is connected to the system or disconnected from the system. 我希望Windows API中有一个通知服务,该服务将告诉我何时音频设备连接到系统与系统断开连接。

Is there such a notification available, or do I have constantly rescan the available audio input devices and create my own notifications when I detect a change? 是否有这样的通知,还是当我检测到更改时是否不断重新扫描可用的音频输入设备并创建自己的通知? I'd obviously don't want to reinvent the wheel. 我显然不想重新发明轮子。

The following will listen for USB plug-in/turn-on un-plug/turn-off at a scan rate of 2seconds intervals 以下内容将以2秒的间隔监听USB插入/打开/关闭/关闭

        //turn on USB device event
        WqlEventQuery q_creation = new WqlEventQuery();
        q_creation.EventClassName = "__InstanceCreationEvent";
        q_creation.WithinInterval = new TimeSpan(0, 0, 2);
        q_creation.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
        mwe_creation = new ManagementEventWatcher(q_creation);
        mwe_creation.EventArrived += new EventArrivedEventHandler(USBEventArrived_Creation);
        mwe_creation.Start();

        //turn off USB device event
        WqlEventQuery q_deletion = new WqlEventQuery();
        q_deletion.EventClassName = "__InstanceDeletionEvent";
        q_deletion.WithinInterval = new TimeSpan(0, 0, 2);
        q_deletion.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'  ";
        mwe_deletion = new ManagementEventWatcher(q_deletion);
        mwe_deletion.EventArrived += new EventArrivedEventHandler(USBEventArrived_Deletion);
        mwe_deletion.Start();    

        private void USBEventArrived_Creation(object sender, EventArrivedEventArgs e)
        {
        }

        private void USBEventArrived_Deletion(object sender, EventArrivedEventArgs e)
        {
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM