处理c#中更改的音频设备事件
本文关键字:音频 事件 处理 | 更新日期: 2023-09-27 17:58:04
当我将耳机或其他输出设备插入(或拔出)声卡插孔时,我想知道如何处理该事件。
在这里和谷歌上搜索可以给我提供关于"naudio"图书馆的信息,但它有非常糟糕的文档需要检查,而且这个项目的一位协调员告诉我,他甚至不确定在他们的图书馆里是否可能。
我的最终目的是自动控制不同设备的音量,例如,当耳机处于活动状态时,设置10%的音量,当扬声器处于活动状态后,设置100%的音量。
您可以使用NAudio的MMDeviceEnumerator和IMMNotificationClient来完成此操作。但是,您添加了RegisterEndpointNotificationCallback&UnRegisterEndpointNotificationCallback到MMDeviceEnumerator类
实现是
/// <summary>
/// Registers a call back for Device Events
/// </summary>
/// <param name="client">Object implementing IMMNotificationClient type casted as IMMNotificationClient interface</param>
/// <returns></returns>
public int RegisterEndpointNotificationCallback([In] [MarshalAs(UnmanagedType.Interface)] IMMNotificationClient client)
{
//DeviceEnum declared below
return deviceEnum.RegisterEndpointNotificationCallback(client);
}
/// <summary>
/// UnRegisters a call back for Device Events
/// </summary>
/// <param name="client">Object implementing IMMNotificationClient type casted as IMMNotificationClient interface </param>
/// <returns></returns>
public int UnRegisterEndpointNotificationCallback([In] [MarshalAs(UnmanagedType.Interface)] IMMNotificationClient client)
{
//DeviceEnum declared below
return deviceEnum.UnregisterEndpointNotificationCallback(client);
}
然后创建一个实现IMMNotificationClient的类
示例:
class NotificationClientImplementation : NAudio.CoreAudioApi.Interfaces.IMMNotificationClient
{
public void OnDefaultDeviceChanged(DataFlow dataFlow, Role deviceRole, string defaultDeviceId)
{
//Do some Work
Console.WriteLine("OnDefaultDeviceChanged --> {0}", dataFlow.ToString());
}
public void OnDeviceAdded(string deviceId)
{
//Do some Work
Console.WriteLine("OnDeviceAdded -->");
}
public void OnDeviceRemoved(string deviceId)
{
Console.WriteLine("OnDeviceRemoved -->");
//Do some Work
}
public void OnDeviceStateChanged(string deviceId, DeviceState newState)
{
Console.WriteLine("OnDeviceStateChanged'n Device Id -->{0} : Device State {1}", deviceId, newState);
//Do some Work
}
public NotificationClientImplementation()
{
//_realEnumerator.RegisterEndpointNotificationCallback();
if (System.Environment.OSVersion.Version.Major < 6)
{
throw new NotSupportedException("This functionality is only supported on Windows Vista or newer.");
}
}
public void OnPropertyValueChanged(string deviceId, PropertyKey propertyKey)
{
//Do some Work
//fmtid & pid are changed to formatId and propertyId in the latest version NAudio
Console.WriteLine("OnPropertyValueChanged: formatId --> {0} propertyId --> {1}", propertyKey.formatId.ToString(), propertyKey.propertyId.ToString());
}
}
那么你所要做的就是
- 声明以下NAudio对象和IMMNotificationClient的实现
样品:
private NAudio.CoreAudioApi.MMDeviceEnumerator deviceEnum = new NAudio.CoreAudioApi.MMDeviceEnumerator();
private NotificationClientImplementation notificationClient;
private NAudio.CoreAudioApi.Interfaces.IMMNotificationClient notifyClient;
- 然后键入cast-notificationClient作为IMMNotificationClient,并将其作为参数传递给MMDeviceEnumerator
样品:
notificationClient = new NotificationClientImplementation();
notifyClient = (NAudio.CoreAudioApi.Interfaces.IMMNotificationClient)notificationClient;
deviceEnum.RegisterEndpointNotificationCallback(notifyClient);
希望这对身体有帮助。感谢MSDN论坛,特别是Michael Taylorhttp://msmvps.com/blogs/p3net谢谢你帮我做这件事。
谢谢&干杯
您将能够确定何时将设备插入系统,您必须通过COM互操作实现IMMNotificationClient
。基本上,您必须定义以下方法的实现:
OnDefaultDeviceChanged
OnDeviceAdded
OnDeviceRemoved
OnDeviceStateChanged
OnPropertyValueChanged
请注意,在以上内容中,您最感兴趣的是:
OnDefaultDeviceChanged
OnDeviceAdded
OnDeviceStateChanged
但是,您应该注意,底层硬件必须支持此功能,并且此功能仅在Windows Vista和Windows Server 2008上可用。
我很确定,插入/拔下耳机或声卡中的任何其他东西都不会产生任何系统事件。