在 Windows 中检测耳机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5526998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect headphones in Windows
提问by Israel Rodriguez
I'm trying to detect if the headphones are plugged in on Windows. I've already tried DirectSound and the NAudio library and have not had any success. Does anyone have any suggestions?
我正在尝试检测耳机是否已插入 Windows。我已经尝试过 DirectSound 和 NAudio 库,但没有任何成功。有没有人有什么建议?
NOTE: this question is a duplicate of this question, but the answer to that question didn't contain any code samples or tutorials.
回答by Israel Rodriguez
So...I think I might found something REALY obscure..
所以......我想我可能会发现一些非常晦涩的东西......
Can any of you guys check if that works? - You MUST have the NAudiolibrary referenced..
你们中的任何人都可以检查一下是否有效吗?- 你必须引用NAudio库..
var enumerator = new MMDeviceEnumerator();
var d = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
Console.WriteLine(d.Properties[new Guid("46d16a2c-5654-41c0-911e-7860d2bce7ee")].Value.ToString());
This property return's 1 (Plugged) or 0 (Unplugged).. and It's working realy good for me..
此属性返回 1(已插入)或 0(未插入)...而且它对我来说非常有用...
UPDATE: Just published the app on CodePlex - iTunesHead
更新:刚刚在CodePlex上发布了该应用程序- iTunesHead
回答by typo.pl
What about Microsoft.DirectX.DirectSound.Speakers?
Microsoft.DirectX.DirectSound.Speakers怎么样?
A Speakers object should have one of the following speaker type properties set to true:
- FivePointOne
- Headphone
- Mono
- Quad
- SevenPointOne
- Stereo
- Surround
Speakers 对象应将以下扬声器类型属性之一设置为 true:
- 五点一
- 耳机
- 单核细胞增多症
- 四边形
- 七点一号
- 立体声
- 环绕