gpt4 book ai didi

c# - 在 C# 中处理更改的音频设备事件

转载 作者:太空狗 更新时间:2023-10-29 22:24:14 27 4
gpt4 key购买 nike

我想知道当我将耳机或其他输出设备插入(或取出)到声卡插孔时如何处理事件。

在这里和谷歌上搜索给我提供了关于“naudio”库的信息,但它的文档非常糟糕,无法检查,而且该项目的一位协调员告诉我他不确定他们的库中是否有可能。

我的最终目的是自动控制不同设备的音量,例如启用耳机时 - 设置 10% 音量,启用扬声器时 - 设置 100%。

最佳答案

您可以使用 NAudio 的 MMDeviceEnumerator 和 IMMNotificationClient 来完成。但是,您将 RegisterEndpointNotificationCallbackUnRegisterEndpointNotificationCallback 的实现添加到 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());
}

}

那么你所要做的就是

  1. 声明以下 NAudio 对象和 IMMNotificationClient 的实现

示例:

private NAudio.CoreAudioApi.MMDeviceEnumerator deviceEnum = new NAudio.CoreAudioApi.MMDeviceEnumerator();
private NotificationClientImplementation notificationClient;
private NAudio.CoreAudioApi.Interfaces.IMMNotificationClient notifyClient;
  1. 然后键入 cast notificationClient as IMMNotificationClient 并将其作为参数传递给 MMDeviceEnumerator

示例:

notificationClient = new NotificationClientImplementation();
notifyClient = (NAudio.CoreAudioApi.Interfaces.IMMNotificationClient)notificationClient;
deviceEnum.RegisterEndpointNotificationCallback(notifyClient);

希望这对 body 有帮助。感谢 MSDN 论坛,尤其是 Michael Taylor http://msmvps.com/blogs/p3net感谢您帮助我。

谢谢和干杯。

关于c# - 在 C# 中处理更改的音频设备事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6163119/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com