using NAudio.Wave;
namespace Record
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
WaveIn waveIn;
WaveFileWriter waveWriter;
private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
if (waveWriter == null) return;
waveWriter.Write(e.Buffer, 0, e.BytesRecorded);
waveWriter.Flush();
}
private void button1_Click(object sender, EventArgs e)
{
waveIn = new WaveIn();
waveIn.DeviceNumber = 0;
string outputFilename = @"D:/demo.wav";
waveIn.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(waveIn.DeviceNumber).Channels);
waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
waveWriter = new WaveFileWriter(outputFilename, waveIn.WaveFormat);
waveIn.StartRecording();
}
private void button2_Click(object sender, EventArgs e)
{
if (waveIn != null)
{
waveIn.StopRecording();
waveIn.Dispose();
waveIn = null;
}
if (waveWriter != null)
{
waveWriter.Dispose();
waveWriter = null;
}
}
}
}