我们可以在录制时使用 waveViever 绘制波形吗?
private NAudio.Wave.WaveIn wi = null;
int waveInDevices = NAudio.Wave.WaveIn.DeviceCount;
WaveFileWriter waveWriter = null;
SaveFileDialog save;
private void button2_Click(object sender, EventArgs e)
{
save = new SaveFileDialog();
save.Filter = "Wave File (*.wav)|*.wav;";
if (save.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
int deviceNumber = 0;
wi = new WaveIn();
wi.DeviceNumber = deviceNumber;
wi.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(deviceNumber).Channels);
wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);
waveWriter = new NAudio.Wave.WaveFileWriter(save.FileName, wi.WaveFormat);
wi.StartRecording();
timer1.Start();
}
private void wi_DataAvailable(object sender, WaveInEventArgs e)
{
if (waveWriter == null) return;
waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded);
waveWriter.Flush();
}
private void timer1_Tick(object sender, EventArgs e)
{
waveViewer1.WaveStream = new WaveFileReader(save.Filename);
}
我正在使用该代码语句记录文件,并希望在计时器的每个滴答间隔读取文件并在 waveViewer 中绘制波形。但是它给出了“文件正在被另一个进程使用”的错误。有没有办法做到这一点。我应该使用 WaveFileReader(Stream streamInput) 的第一个重载吗?如果是,请你举一个关于第一次重载的例子。