NAudio 枚举音频输入输出设备

使用 VS2019 C#,安装 NAudio库后在  Console 下面枚举 Audio Input 和 Output 的代码:

完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using NAudio;
using NAudio.CoreAudioApi;
using MMDevice = NAudio.CoreAudioApi.MMDevice;
using MMDeviceEnumerator = NAudio.CoreAudioApi.MMDeviceEnumerator;

using NAudio.Wave;

namespace ConsoleApp1
{
    class Program
    {

        static void Main(string[] args)
        {
            //定义一个 dataFlow对象
            MMDeviceEnumerator enumerator = new MMDeviceEnumerator();

            IEnumerable<MMDevice> playBackList;
            //获取声音输出设备
            playBackList = enumerator.EnumerateAudioEndPoints(DataFlow.Render, NAudio.CoreAudioApi.DeviceState.Active).ToArray();

            Console.WriteLine("Output device list:");
            foreach (var ad in playBackList)
            {
                Console.WriteLine("   "+ad.FriendlyName.ToString());
            }

            IEnumerable < MMDevice > captureList;
            captureList = enumerator.EnumerateAudioEndPoints(DataFlow.Capture, NAudio.CoreAudioApi.DeviceState.Active).ToArray();;
            Console.WriteLine("Input device list:");
            foreach (var ad in captureList)
            {
                Console.WriteLine("   " + ad.FriendlyName.ToString());
            }

            // 更简单的方法
for (int n = 0; n < WaveOut.DeviceCount; n++)
            {
                Console.WriteLine(n + " : " + WaveOut.GetCapabilities(n).ProductName);
            }
            for (int n = 0; n < WaveIn.DeviceCount; n++)
            {
                Console.WriteLine(n + " : " + WaveIn.GetCapabilities(n).ProductName);
            }


            Console.ReadLine();

        }
    }
}

运行结果:

在系统中直接查看:

参考:

  1. https://blog.csdn.net/u011465910/article/details/127859286   C# Audio全自动化测试——1. 枚举Audio设备
  2. https://cloud.tencent.com/developer/information/%E5%9C%A8%E6%B7%BB%E5%8A%A0%2F%E5%88%A0%E9%99%A4%E5%A3%B0%E9%9F%B3%E8%AE%BE%E5%A4%87%E5%90%8E%EF%BC%8C%E5%A6%82%E4%BD%95%E5%9C%A8NAudio%E4%B8%AD%E9%80%89%E6%8B%A9%E6%AD%A3%E7%A1%AE%E7%9A%84%E5%A3%B0%E9%9F%B3%E8%BE%93%E5%87%BA%E8%AE%BE%E5%A4%87%EF%BC%9F 在添加/删除声音设备后,如何在NAudio中选择正确的声音输出设备?
  3. https://blog.csdn.net/LiChangGG/article/details/100132901