如何使用C#和NAudio从USB麦克风获取音频输入

时间:2017-09-12 17:33:52

标签: c# .net usb wav naudio

我试图使用NAudio从Usb设备获取音频输入。 我正在使用WavIn类,但是喜欢这样:

NAudio.Wave.WaveIn input = new NAudio.Wave.WaveIn();
input.DeviceNumber=0;

但我不知道如何获取微型设备号。 我知道这个方法:

WaveIn.GetCapabilities();

但是没有我的微观,因为它不被认为是微电话。 我也可以像我这样得到我的USB设备:

    System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(@"Select * From Win32_USBHub");
    var devices = searcher.Get();
    foreach(var d in devices)
    {
        var deviceId = (string)d.GetPropertyValue("DeviceID");
        var pnpDeviceID = (string)d.GetPropertyValue("PNPDeviceID");
        var description = (string)d.GetPropertyValue("Description");
    }

但我无法将其传递给WaveIn。

如何使用NAudio获取usb设备输入?

1 个答案:

答案 0 :(得分:0)

如果您想使用WaveIn进行录制,则需要将其显示在可用的WaveIn设备列表中:

for (int n = 0; n < WaveIn.DeviceCount; n++)
{
    var caps = WaveIn.GetCapabilities(n);
    Console.WriteLine($"{n}: {caps.ProductName} {caps.Channels}");
}

或者,可以使用WasapiCapture

访问它
var deviceEnum = new MMDeviceEnumerator();

foreach (var device in deviceEnum.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active))
{
    Console.WriteLine($"{device.ID}: {device.DeviceFriendlyName} / {device.FriendlyName}");
}