简体   繁体   English

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

[英]How to get audio input from usb microphone with C# and NAudio

I'm trying to get audio input from Usb device using NAudio. 我正在尝试使用NAudio从USB设备获取音频输入。 I'm using WavIn class but like this: 我正在使用WavIn类,但是像这样:

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

But I don't know how to get device number of my micro. 但是我不知道如何获取微型设备的设备号。 I know about this method: 我知道这种方法:

WaveIn.GetCapabilities();

But there is no my micro, becouse it is not recognized as micrphone. 但是没有我的麦克风,因为它不被认为是麦克风。 Also I can get my usb device like thi: 我也可以像这样购买我的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");
    }

But I can't pass it to WaveIn. 但是我无法将其传递给WaveIn。

How can I get usb device input using NAudio? 如何使用NAudio获得USB设备输入?

Well if you want to record from it with WaveIn you need it to appear in the list of available WaveIn devices: 好吧,如果您想使用WaveIn从其中进行录制,则需要将其显示在可用WaveIn设备的列表中:

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

Alternatively, it may be accessible with WasapiCapture : 或者,可以使用WasapiCapture访问WasapiCapture

var deviceEnum = new MMDeviceEnumerator();

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

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

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