Enumerate Recording Devices in NAudio

前端 未结 2 878
暖寄归人
暖寄归人 2021-02-06 00:02

How can you get a list of all the recording devices on a computer using NAudio? When you want to record, you have to give it the index of the device you want to use, but there\

2条回答
  •  梦谈多话
    2021-02-06 00:21

    For WaveIn, you can use the static WaveIn.GetCapabilities method. This will give you a device name, but with the annoying limitation that it is a maximum of 31 characters. I am still looking for the way to get the full name (see my question 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);
    }
    

    For WASAPI (Vista and above), you can use the MMDeviceEnumerator:

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

    I tend to recommend WaveIn, as it is more widely supported, and allows more flexibility over recording sample rates.

提交回复
热议问题