枚举NAudio中的录音设备

16

如何使用NAudio获取计算机上所有录音设备的列表?当您想要录制时,必须提供要使用的设备的索引,但没有办法知道那个设备是什么。我希望能够从麦克风、立体声混音等选取。

2个回答

33

对于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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接