是否有任何方法可以通过编程方式确定何时将耳机插入计算机?
基本上,我只想写一个简单的工具,除非插入耳机,否则我的声音会静音.这甚至可能吗?
没有示例如何通过C#检测耳机是否插入.
我认为应该是一些事件......
使用WMI有意义吗?
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\cimv2",
"SELECT * FROM Win32_SoundDevice");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_SoundDevice instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]);
}
Run Code Online (Sandbox Code Playgroud)
有人会很高兴提供它吗?
谢谢!
我正在使用惊人的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());
Run Code Online (Sandbox Code Playgroud)
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) …Run Code Online (Sandbox Code Playgroud) 我正在编写一个能够录制麦克风音频的应用程序.我的问题是我需要知道用户何时插入插孔并拔出插孔.
我查看了Win32_SoundDeviceWMI课程,在我看来,没有这样的属性,我可以检查杰克的状态.
后来我发现RegisterEndpointNotificationCallback在IMMDeviceEnumerator这似乎做什么,我需要,但我不知道如何做到这一点在C#.有谁知道如何使用MMDeviceEnumerator方法来检查音频端口状态?任何帮助高度赞赏.
c# ×3
.net ×2
audio ×2
audio-device ×2
headphones ×2
hardware ×1
io ×1
microphone ×1
naudio ×1