我正在使用 NAudio 库在 C# 中捕获音频输入:
int _deviceID;
double _previewVal;
private void PreviewData()
{
WaveIn waveIn = new WaveIn();
waveIn.DeviceNumber = _deviceID;
waveIn.DataAvailable += waveIn_DataAvailable;
int sampleRate = 8000; // 8 kHz
int channels = 1; // mono
waveIn.WaveFormat = new WaveFormat(sampleRate, channels);
waveIn.StartRecording();
}
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
for (int index = 0; index < e.BytesRecorded; index += 2)
{
short sample = (short)((e.Buffer[index + 1] << 8) |
e.Buffer[index + 0]);
_previewVal = sample / 327.68f;
}
}
为每个音频输入设备(在我的系统中为 4 个)同时调用函数 PreviewData。当我只为一个设备调用该方法时,它似乎正在工作,但如果我再次调用它,我会得到异常“AlreadyAllocated 调用 waveInOpen”。有人知道如何解决这个问题吗?