Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c语言中处理已更改的音频设备事件#_C#_.net_Events_Audio_Device - Fatal编程技术网

C# 在c语言中处理已更改的音频设备事件#

C# 在c语言中处理已更改的音频设备事件#,c#,.net,events,audio,device,C#,.net,Events,Audio,Device,当我将耳机或其他输出设备插入(或拔出)声卡插孔时,我想知道如何处理该事件 在这里和谷歌上搜索给我提供了关于“naudio”图书馆的信息,但它有非常糟糕的文档需要检查,而且这个项目的一位协调人告诉我,他甚至不确定在他们的图书馆中是否可能 我的最终目的是自动控制不同设备的音量,例如,当耳机处于活动状态时-设置10%的音量,当扬声器处于活动状态时-设置100%。您将能够确定设备何时插入系统,您必须通过COM互操作来实现。基本上,您必须定义以下方法的实现: 请注意,在上述各项中,您最感兴趣

当我将耳机或其他输出设备插入(或拔出)声卡插孔时,我想知道如何处理该事件

在这里和谷歌上搜索给我提供了关于“naudio”图书馆的信息,但它有非常糟糕的文档需要检查,而且这个项目的一位协调人告诉我,他甚至不确定在他们的图书馆中是否可能


我的最终目的是自动控制不同设备的音量,例如,当耳机处于活动状态时-设置10%的音量,当扬声器处于活动状态时-设置100%。

您将能够确定设备何时插入系统,您必须通过COM互操作来实现。基本上,您必须定义以下方法的实现:

请注意,在上述各项中,您最感兴趣的是:

  • OnDefaultDeviceChanged
  • OnDeviceAdded
  • OnDeviceStateChanged

但是,您应该知道,底层硬件必须支持此功能,这只在Windows Vista和Windows Server 2008上可用。

我非常确定,在声卡中插入/拔出耳机或其他任何东西不会产生任何系统事件。

您可以使用NAudio的MMDeviceEnumerator和IMNotificationClient来完成。但是,您可以将RegisterEndpointNotificationCallbackUnRegisterEndpointNotificationCallback的实现添加到MMDeviceEnumerator类中

实现是

 /// <summary>
       /// Registers a call back for Device Events
       /// </summary>
        /// <param name="client">Object implementing IMMNotificationClient type casted as IMMNotificationClient interface</param>
       /// <returns></returns>
        public int RegisterEndpointNotificationCallback([In] [MarshalAs(UnmanagedType.Interface)] IMMNotificationClient client)
        {
            //DeviceEnum declared below
            return deviceEnum.RegisterEndpointNotificationCallback(client);
        }

        /// <summary>
        /// UnRegisters a call back for Device Events
        /// </summary>
        /// <param name="client">Object implementing IMMNotificationClient type casted as IMMNotificationClient interface </param>
        /// <returns></returns>
        public int UnRegisterEndpointNotificationCallback([In] [MarshalAs(UnmanagedType.Interface)] IMMNotificationClient client)
        {
            //DeviceEnum declared below
            return deviceEnum.UnregisterEndpointNotificationCallback(client);
        } 
  • 然后键入cast notificationClient作为imnotificationclient,并将其作为参数传递给MMDeviceEnumerator
  • 样本:

    private NAudio.CoreAudioApi.MMDeviceEnumerator deviceEnum = new NAudio.CoreAudioApi.MMDeviceEnumerator();
    private NotificationClientImplementation notificationClient;
    private NAudio.CoreAudioApi.Interfaces.IMMNotificationClient notifyClient;
    
    notificationClient = new NotificationClientImplementation();
    notifyClient = (NAudio.CoreAudioApi.Interfaces.IMMNotificationClient)notificationClient;
    deviceEnum.RegisterEndpointNotificationCallback(notifyClient);
    
    希望这对身体有帮助。感谢MSDN论坛,特别是迈克尔·泰勒 谢谢你帮我做这件事


    谢谢和干杯。

    好的,我只要收到关于“某物”的信息就足够了。这怎么可能?我还尝试了另一种方法—在windows事件查看器中搜索此事件,但我一无所获—可能是我搜索得不太好,也可能是此事件没有记录在那里。@xapon:不幸的是,我说“最多”你可以获得该信息。如果您这样做,它将特定于声卡或主板(如果插孔集成);我不确定硬件抽象层是否公开了一个标准事件,您可以从中获取它;例如,下面是用于跟踪音频事件的windows界面。所以,如果我能深入一点,我会找到方法。但我希望已经有一些抽象,如库或类,可以帮助我。无论如何,感谢您的解释。@xapon-扩展了我的答案,最终,您必须使用COM互操作来实现此设备,然后注册您的实现。非常感谢您的回答!不幸的是,我问这个问题已经三年了,我很久以前就放弃了这个课题的研究。但我希望它能帮助其他人解决同样的问题。@Mohtashim非常感谢你!我已经搜索了几个小时的实现示例!我会竖起大拇指,这100倍的样品以及详细的答案!!我使用这里描述的相同方式,我只在插入耳机插孔时看到OnPropertyChange事件运行,在拔下耳机插孔时看不到任何内容。原因是什么?
    notificationClient = new NotificationClientImplementation();
    notifyClient = (NAudio.CoreAudioApi.Interfaces.IMMNotificationClient)notificationClient;
    deviceEnum.RegisterEndpointNotificationCallback(notifyClient);