在Windows中检测笔记本电脑上的耳机

时间:2016-10-12 10:19:01

标签: c# .net windows desktop-application naudio

我目前正在开发一个Windows桌面应用程序,应该会在连接的音频设备上发生任何变化时收到通知。

我目前正通过NAudio包以下列方式使用Core Audio Interface API:

NAudio.CoreAudioApi.MMDeviceEnumerator enumerator;   

public void PopulateAudioDevices()
{
    enumerator = new NAudio.CoreAudioApi.MMDeviceEnumerator();

    var endpoints = enumerator.EnumerateAudioEndPoints(
        DataFlow.Render,
        DeviceState.Unplugged | DeviceState.Active);
}

protected override void OnStart()
{
    PopulateAudioDevices();

    if (enumerator != null)
    {
        enumerator.RegisterEndpointNotificationCallback(this);
    }
}

public void OnDeviceStateChanged(string deviceId, DeviceState newState)
{
    Console.Writeline($"{deviceName} : Device State {newState}");
}

这似乎适用于大多数USB设备,但似乎有些情况下,通过笔记本电脑上的外部耳机插孔连接设备不会导致OnDeviceStateChanged回调被触发。我注意到的一个区别因素是,在这些情况下,当插入外部音频设备时,Windows“设备管理器”屏幕上没有可见的变化。但我注意到,无论何时插入音频设备,Windows音量设置都会发生变化这使我相信操作系统在某种程度上注定了连接设备的差异。

有没有办法检测到这种情况?

0 个答案:

没有答案