问题
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.
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();
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.
How do I get the default device?
回答1:
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.
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,
来源:https://stackoverflow.com/questions/26077907/get-default-output-audio-device-with-naudio