Get default output audio device with NAudio

ε祈祈猫儿з 提交于 2019-12-22 18:09:00

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!