gpt4 book ai didi

c# - 如何区分耳机与PC中的集成音频

转载 作者:太空狗 更新时间:2023-10-29 17:53:21 25 4
gpt4 key购买 nike

我正在使用惊人的 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) { }
}

最佳答案

发布的片段不完整,您需要查看(可能是新的)默认音频设备的设备属性。特别是外形尺寸,您需要检测头戴式耳机或耳机。大致是这样的:

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...

}

关于c# - 如何区分耳机与PC中的集成音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34024205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com