i m 使用 naudio (使用 naudio ) 进行音频编程 。 Im my 程序 ( C#. net winForms) 我有记录和停止按钮。 代码看起来像 :
NAudio.Wave.WaveIn sourceStream = null;
NAudio.Wave.WaveFileWriter waveWriter = null;
private void RecordButton_Click(object sender, EventArgs e)
{
int deviceNumber = 0;
sourceStream = new NAudio.Wave.WaveIn();
sourceStream.DeviceNumber = deviceNumber;
sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);
sourceStream.DataAvailable += new EventHandler<NAudio.Wave.WaveInEventArgs>(sourceStream_DataAvailable);
waveWriter = new NAudio.Wave.WaveFileWriter("d:\a.wav", sourceStream.WaveFormat);
sourceStream.StartRecording();
}
private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
{
if (waveWriter == null) return;
waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded);
waveWriter.Flush();
}
private void StopButton_Click(object sender, EventArgs e)
{
waveWriter.Dispose();
waveWriter = null;
}
它有效,但波文件质量不好, 音响i 记录有小差距。 我要样本率44100赫兹和样本格式16比特。