简体   繁体   English

使用NAudio获取默认的输出音频设备

[英]Get default output audio device with NAudio

I want to get the default output audio device (ie my speakers) using NAudio, to get the master sound volume as in this question . 我想使用NAudio获取默认的输出音频设备(即我的扬声器),以获取此问题中的主音量。

I am trying to use MMDeviceEnumerator.GetDevice() , but the id it takes is a string, not the device number. 我正在尝试使用MMDeviceEnumerator.GetDevice() ,但是它使用的ID是一个字符串,而不是设备号。 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. 我尝试过将各种Guid从功能以及字符串格式的设备ID传递到GetDevice()但是它们都不起作用。

How do I get the default device? 如何获得默认设备?

You are mixing two completely different audio APIs here. 您在这里混合了两种完全不同的音频API。 MMDeviceEnumerator is part of WASAPI, the new audio API introduced in WindowsVista, and WaveOut.DeviceCount uses the old Windows audio APIs. MMDeviceEnumerator是WASAPI(WindowsVista中引入的新音频API)的一部分,WaveOut.DeviceCount使用旧的Windows音频API。

To use WASAPI to get the default audio device, you use code like this: 要使用WASAPI获取默认音频设备,请使用如下代码:

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,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM