我想使用NAudio获取默认输出音频设备(即我的扬声器),以获得此问题中的主音量.
我正在尝试使用MMDeviceEnumerator.GetDevice(),但它所需的ID是一个字符串,而不是设备号.这是我到目前为止编写的代码:
var enumerator = new MMDeviceEnumerator();
for (int i = 0; i < WaveOut.DeviceCount; i++)
{
var cap = WaveOut.GetCapabilities(i);
Console.WriteLine("{0}: {1}", i, cap.ProductName);
var device = enumerator.GetDevice(???);
}
Console.WriteLine();
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
我已经尝试从功能中传递各种Guids,以及以字符串格式传递设备ID,GetDevice()但它们都不起作用.
如何获取默认设备?
您在这里混合了两个完全不同的音频API.MMDeviceEnumerator是WASAPI的一部分,WASAPI是WindowsVista中引入的新音频API,WaveOut.DeviceCount使用旧的Windows音频API.
要使用WASAPI获取默认音频设备,请使用以下代码:
var enumerator = new MMDeviceEnumerator();
enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
Run Code Online (Sandbox Code Playgroud)
实际上有三种不同类型的默认音频输出设备,具体取决于目的(角色):
/// <summary>
/// Games, system notification sounds, and voice commands.
/// </summary>
Console,
/// <summary>
/// Music, movies, narration, and live music recording
/// </summary>
Multimedia,
/// <summary>
/// Voice communications (talking to another person).
/// </summary>
Communications,
Run Code Online (Sandbox Code Playgroud)