【问题标题】:Enumerate Recording Devices in NAudio枚举 NAudio 中的录音设备
【发布时间】:2009-09-19 18:10:43
【问题描述】:

如何使用 NAudio 获取计算机上所有录音设备的列表?当你想记录时,你必须给它你要使用的设备的索引,但是没有办法知道那是什么设备。我希望能够从 Mic、Stereo Mix 等中进行选择。

【问题讨论】:

    标签: c# naudio


    【解决方案1】:

    对于 WaveIn,您可以使用静态 WaveIn.GetCapabilities 方法。这将为您提供设备名称,但令人讨厌的限制是最多 31 个字符。我仍在寻找获得全名的方法(请参阅我的问题here)。

    int waveInDevices = WaveIn.DeviceCount;
    for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
    {
        WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
        Console.WriteLine("Device {0}: {1}, {2} channels", waveInDevice, deviceInfo.ProductName, deviceInfo.Channels);
    }
    

    对于 WASAPI(Vista 及以上),您可以使用 MMDeviceEnumerator:

    MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
    foreach (MMDevice device in enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All))
    {
        Console.WriteLine("{0}, {1}", device.FriendlyName, device.State);
    }
    

    我倾向于推荐 WaveIn,因为它受到更广泛的支持,并且在记录采样率方面具有更大的灵活性。

    【讨论】:

      【解决方案2】:

      要获得完整的设备名称,我使用这个...

      using NAudio.CoreAudioApi;
      using NAudio.Wave;
      

      获取所有录音设备:

      //create enumerator
      var enumerator = new MMDeviceEnumerator();
      //cycle through all audio devices
      for (int i = 0; i < WaveIn.DeviceCount; i++)
          Console.WriteLine(enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active)[i]);
      //clean up
      enumerator.Dispose();
      

      获取所有捕获设备:

      //create enumerator
      var enumerator = new MMDeviceEnumerator();
      //cyckle trough all audio devices
      for (int i = 0; i < WaveOut.DeviceCount; i++)
          Console.WriteLine(enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active)[i]);
      //clean up
      enumerator.Dispose();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多