【问题标题】:Receive notification in C# app when a new USB audio device is connected or disconnected?连接或断开新的 USB 音频设备时在 C# 应用程序中接收通知?
【发布时间】:2015-08-22 17:30:48
【问题描述】:

我有一个在 Windows 8.1 或更高版本上运行并进行语音识别的 C# Windows 窗体 应用程序。我想在新的 USB 音频输入设备连接到系统时收到通知。我希望 Windows API 中有一个通知服务,它会告诉我音频设备何时连接到系统与系统断开连接。

是否有这样的通知可用,或者我是否经常重新扫描可用的音频输入设备并在检测到更改时创建自己的通知?我显然不想重新发明轮子。

【问题讨论】:

标签: c# winapi audio notifications usb


【解决方案1】:

下面会以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)
        {
        }

【讨论】:

    猜你喜欢
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2012-10-05
    • 2023-04-02
    • 2022-12-16
    • 2013-10-26
    相关资源
    最近更新 更多