【问题标题】:How to difference headphones from integrated audio in PC如何区分耳机和PC中的集成音频
【发布时间】:2016-03-05 14:24:13
【问题描述】:

我正在使用惊人的NAudio 框架来获取音频设备的列表。

但正如我所见,哪个音频设备是 PC 的集成音频,哪个是耳机,这是不可能的区别。我的意思是它们具有相同的名称,并且只有当我们插入耳机时,它才会进入Active 状态。

想象一下,如果我在插入耳机的情况下启动应用程序我如何知道当前设备是耳机而不是 PC 的集成音频?

我的意思是我们可以通过 NAduio 检测到插入的音频设备是外部音频设备并且本身就是耳机吗?

var enumerator = new NAudio.CoreAudioApi.MMDeviceEnumerator();

// Allows you to enumerate rendering devices in certain states
var endpoints = enumerator.EnumerateAudioEndPoints(
    DataFlow.Render,
    DeviceState.Unplugged | DeviceState.Active);
foreach (var endpoint in endpoints)
{
    Console.WriteLine("{0} - {1}", endpoint.DeviceFriendlyName, endpoint.State);
}

// Aswell as hook to the actual event
enumerator.RegisterEndpointNotificationCallback(new NotificationClient());

NotificationClient 的实现方式如下:

class NotificationClient : NAudio.CoreAudioApi.Interfaces.IMMNotificationClient
{
    void IMMNotificationClient.OnDeviceStateChanged(string deviceId, DeviceState newState)
    {
        Console.WriteLine("OnDeviceStateChanged\n Device Id -->{0} : Device State {1}", deviceId, newState);
    }

    void IMMNotificationClient.OnDeviceAdded(string pwstrDeviceId) { }
    void IMMNotificationClient.OnDeviceRemoved(string deviceId) { }
    void IMMNotificationClient.OnDefaultDeviceChanged(DataFlow flow, Role role, string defaultDeviceId) { }
    void IMMNotificationClient.OnPropertyValueChanged(string pwstrDeviceId, PropertyKey key) { }
}

【问题讨论】:

  • 为什么需要知道设备是外置的还是集成的?

标签: c# naudio headphones audio-device naudio-framework


【解决方案1】:

贴出来的sn-p不完整,需要查看(可能是新的)默认音频设备的设备属性。特别是外形尺寸,您需要检测耳机或耳机。大致是这样的:

void IMMNotificationClient.OnDeviceStateChanged(string deviceId, DeviceState newState) {
    Console.WriteLine("OnDeviceStateChanged\n Device Id -->{0} : Device State {1}", deviceId, newState);
    var endp = new NAudio.CoreAudioApi.MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
    bool isHeadPhone = false;
    PropertyKey key = PropertyKeys.PKEY_AudioEndpoint_FormFactor;
    var store = endp.Properties;
    for (var index = 0; index < store.Count; index++) {
        if (store.Get(index).Equals(key)) {
            var value = (uint)store.GetValue(index).Value;
            const uint formHeadphones = 3;
            const uint formHeadset = 5;
            if (value == formHeadphones || value == formHeadset) {
                isHeadPhone = true;
                break;
            }
        }
    }
    // Use isHeadPhone
    // etc...

}

【讨论】:

    【解决方案2】:

    This 项目似乎可以满足您的需求。但是,根据我的研究,您尝试做的似乎是所谓的“杰克检测”,所以也许这将有助于您进一步搜索。

    来自链接的项目:

    项目描述这是一个简单的iTunes连接应用程序,暂停 拔下耳机时的音乐。最后编辑于 2011 年 4 月 2 日 晚上 11:24,yzraeu,第 2 版

    它不使用 NAUDIO,但最相关的代码在这里:

    const string HEAD_PHONE_GUID = "46d16a2c-5654-41c0-911e-7860d2bce7ee";
    const string HEAD_PHONE_PLUGGED_VALUE = "1";
    
    void CheckHeadphone()
            {
                var device = new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
                if (device.Properties[new Guid(HEAD_PHONE_GUID)].Value.ToString() == HEAD_PHONE_PLUGGED_VALUE)
                    IsHeadphonePlugged = true;
                else
                    IsHeadphonePlugged = false;
            }
    

    一切背后真正的“魔力”是 MMDeviceEnumerator,here 对此有更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      相关资源
      最近更新 更多