using NAudio.Wave;
namespace Recorder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
WaveIn waveIn;
WaveFileWriter writer;
string outputFilename = "D:/demo.wav";
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
writer.Write(e.Buffer, 0, e.BytesRecorded);
}
void waveIn_RecordingStopped(object sender, EventArgs e)
{
waveIn.Dispose();
waveIn = null;
writer.Close();
writer = null;
}
private void button1_Click(object sender, EventArgs e)
{
waveIn = new WaveIn();
waveIn.DeviceNumber = 0;
waveIn.DataAvailable += waveIn_DataAvailable;
waveIn.RecordingStopped += new EventHandler<NAudio.Wave.StoppedEventArgs>(waveIn_RecordingStopped);
waveIn.WaveFormat = new WaveFormat(44100, 2);
writer = new WaveFileWriter(outputFilename, waveIn.WaveFormat);
waveIn.StartRecording();
}
private void button2_Click(object sender, EventArgs e)
{
waveIn.StopRecording();
}
}
}
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;
}
}
}
}