在 C# 中处理更改的音频设备事件
我想知道当我将耳机或其他输出设备插入(或拔出)声卡插孔时如何处理该事件。
在这里和谷歌上搜索为我提供了有关“naudio”库的信息,但它的文档非常差,而且该项目的协调员之一告诉我,他甚至不确定在他们的库中是否有可能。
我的最终目的是自动控制不同设备的音量,例如,当耳机处于活动状态时 - 设置 10% 音量,当扬声器处于活动状态时 - 设置 100%。
I'm wondering how to process the event when I insert (or extract) my headphones or another output device to soundcard jack.
Searching here and on google gives me information about "naudio" library, but it has very poor documentation to examine and also one of coordinators of this project told me he isn't sure than it even possible in their library.
My eventual purpose is automatic controlling volume for different devices, e.g. when headphones are active - set 10% volume, and when speakers are active - set 100%.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 NAudio 的 MMDeviceEnumerator 和 IMMNotificationClient 来完成此操作。但是,您可以添加 RegisterEndpointNotificationCallback 的实现 & UnRegisterEndpointNotificationCallback 到 MMDeviceEnumerator 类
实现是
然后创建一个实现 IMMNotificationClient
示例的类:
然后您所要做的就是
示例的实现:
示例:
希望这对某些人有帮助。感谢 MSDN 论坛,特别是 Michael Taylor
http://msmvps.com/blogs/p3net 帮助我解决这个问题。
谢谢&干杯。
You can do it using NAudio's MMDeviceEnumerator and IMMNotificationClient. However you add Implementations for RegisterEndpointNotificationCallback & UnRegisterEndpointNotificationCallback to MMDeviceEnumerator Class
The implementations are
Then create a class that implements IMMNotificationClient
sample:
Then all you have to do is
Sample:
Sample:
Hope this helps some body. Thanks to MSDN Forums and particularly Michael Taylor
http://msmvps.com/blogs/p3net for helping me with this.
Thanks & Cheers.
您将能够确定设备何时插入系统,您必须实施
IMMNotificationClient
通过 COM 互操作。基本上,您必须定义以下方法的实现:OnDefaultDeviceChanged
OnDeviceAdded
OnDeviceRemoved
OnDeviceStateChanged
OnPropertyValueChanged
请注意,在上述内容中,您最感兴趣的是:
OnDefaultDeviceChanged
OnDeviceAdded
OnDeviceStateChanged
但是,您应该注意底层硬件必须支持此功能,并且此功能仅在 Windows Vista 和 Windows Server 2008 上可用。
You will be able to determine when a device is plugged into the system, you will have to implement the
IMMNotificationClient
through COM interop. Basically, you will have to define the implementations of the following methods:OnDefaultDeviceChanged
OnDeviceAdded
OnDeviceRemoved
OnDeviceStateChanged
OnPropertyValueChanged
Note that of the above, the ones you are mostly interested in are:
OnDefaultDeviceChanged
OnDeviceAdded
OnDeviceStateChanged
However, you should be aware that the underlying hardware has to support this functionality, and that this is only available on Windows Vista and Windows Server 2008 on.
我修改了上面的 @randall-deetz 代码以使其自包含,您可以将其直接放入使用 NAudio 的任何应用程序中。感谢您提供了一个很棒的示例,它对我的项目确实有帮助!
I modified @randall-deetz code above to make it self contained and you can drop this right into any application using NAudio. Thanks for a great sample, it really helped me with my project!
我非常确定插入/拔出耳机或声卡中的其他任何东西不会生成任何系统事件
I was pretty sure that plugging/unplugging headphones or anything else in audiocard doesn't generate any system event whatsoever