我对 NAudio 很陌生,正在尝试编写一个小程序来记录来自麦克风的一些音频并将其写入 Wave 文件。当我在程序的 Main 函数中调用 recordStuff() 函数时,程序无法通过 WaveIn 对象的构造,并且程序因 InvalidOperationException 而死,其中显示消息“使用 WaveInEvent 在后台线程上录制” . 了解 NAudio 的人可以告诉我这意味着什么吗?我曾尝试在它自己的线程中调用 recordStuff() 函数,但最终得到相同的结果。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAudio.Wave;
namespace SimpleWave
{
class Recorder
{
public Recorder()
{
writer = new WaveFileWriter("C:\\Users\\Paul\\Desktop\\yeah.wav", new NAudio.Wave.WaveFormat(44100, 1));
}
public static List<byte> buff = new List<byte>();
public static WaveFileWriter writer;
public void recordStuff()
{
// WaveIn Streams for recording
WaveIn waveInStream;
waveInStream = new WaveIn();
waveInStream.DeviceNumber = 0;
waveInStream.WaveFormat = new WaveFormat(44100, 2);
writer = new WaveFileWriter(@"C:\Users\Paul\Desktop\this.wav", waveInStream.WaveFormat);
waveInStream.DataAvailable += new EventHandler<WaveInEventArgs>(waveInStream_DataAvailable);
waveInStream.StartRecording();
}
public void waveInStream_DataAvailable(object sender, WaveInEventArgs e)
{
writer.Write(e.Buffer, 0, e.BytesRecorded);
}
}
}