获取带有NAudio的默认输出音频设备
我想使用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();
我已经尝试将各种Guids从function以及字符串格式的设备ID传递给GetDevice()
但它们都不起作用。
如何获取默认设备?
您在这里混合了两个完全不同的音频API。 MMDeviceEnumerator是WASAPI的一部分,WASAPI是WindowsVista中引入的新音频API,WaveOut.DeviceCount使用旧的Windows音频API。
要使用WASAPI获取默认音频设备,请使用以下代码:
var enumerator = new MMDeviceEnumerator(); enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
实际上有三种不同类型的默认音频输出设备,具体取决于目的(角色):
/// /// Games, system notification sounds, and voice commands. /// Console, /// /// Music, movies, narration, and live music recording /// Multimedia, /// /// Voice communications (talking to another person). /// Communications,