使用NAudio获取默认的输出音频设备 [英] Get default output audio device with NAudio
问题描述
我想使用NAudio获取默认的输出音频设备(即我的扬声器),以获取
I want to get the default output audio device (i.e. my speakers) using NAudio, to get the master sound volume as in this question.
我正在尝试使用MMDeviceEnumerator.GetDevice()
,但是它使用的ID是一个字符串,而不是设备号.这是我到目前为止编写的代码:
I am trying to use MMDeviceEnumerator.GetDevice()
, but the id it takes is a string, not the device number. Here's the code I've written so far:
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();
我尝试将各种Guid从功能以及字符串格式的设备ID传递给GetDevice()
,但是它们都不起作用.
I've tried passing the various Guids from the capabilities, as well as the device id in string format, to GetDevice()
but none of them work.
如何获取默认设备?
推荐答案
您在这里混合使用了两种完全不同的音频API. MMDeviceEnumerator是WASAPI的一部分,WASAPI是WindowsVista中引入的新音频API,WaveOut.DeviceCount使用旧的Windows音频API.
You are mixing two completely different audio APIs here. MMDeviceEnumerator is part of WASAPI, the new audio API introduced in WindowsVista, and WaveOut.DeviceCount uses the old Windows audio APIs.
要使用WASAPI来获取默认的音频设备,请使用以下代码:
To use WASAPI to get the default audio device, you use code like this:
var enumerator = new MMDeviceEnumerator();
enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
实际上有三种不同类型的默认音频输出设备,具体取决于目的(角色):
There are actually three different types of default audio output device, depending on the purpose (role):
/// <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,
这篇关于使用NAudio获取默认的输出音频设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!