如何区分耳机与PC中的集成音频
How to difference headphones from integrated audio in PC
我正在使用惊人的 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) { }
}
This 项目似乎可以满足您的需求。但是,根据我的研究,您尝试做的事情似乎叫做 "jack detection",所以这可能会帮助您进一步搜索。
来自链接项目:
Project Description It's a simples iTunes connected app, to pause
music when the headphones are unplugged. Last edited Apr 2, 2011 at
11:24 PM by yzraeu, version 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;
}
一切背后真正的 "magic" 是 MMDeviceEnumerator,here 是关于它的更多信息。
发布的代码片段不完整,您需要查看(可能是新的)默认音频设备的设备属性。特别是外形尺寸,您需要检测头戴式耳机或耳机。大致是这样的:
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...
}
我正在使用惊人的 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) { }
}
This 项目似乎可以满足您的需求。但是,根据我的研究,您尝试做的事情似乎叫做 "jack detection",所以这可能会帮助您进一步搜索。
来自链接项目:
Project Description It's a simples iTunes connected app, to pause music when the headphones are unplugged. Last edited Apr 2, 2011 at 11:24 PM by yzraeu, version 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;
}
一切背后真正的 "magic" 是 MMDeviceEnumerator,here 是关于它的更多信息。
发布的代码片段不完整,您需要查看(可能是新的)默认音频设备的设备属性。特别是外形尺寸,您需要检测头戴式耳机或耳机。大致是这样的:
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...
}