Call micStart with your required duration (ie 15 seconds) in your if statement
Wave header class courtesy of
http://www.codeproject.com/Members/JoelIvoryJohnson
[
^]
From his article
Writing a Proper Wave File[
^]
class MicrophoneRec {
private Timer timer = null;
private Microphone mic = Microphone.Default;
List<byte[]> byteList = new List<byte[]>();
private string outPutFile = @"D:\TempDir\test.wav";
public void micStart(int durationToRecordSec) {
if (mic != null) {
timer = new Timer();
timer.Interval = (durationToRecordSec * 1000);
timer.Elapsed += new ElapsedEventHandler(t_Elapsed);
mic.BufferDuration = TimeSpan.FromMilliseconds(100);
mic.BufferReady += new EventHandler<EventArgs>(mic_BufferReady);
timer.Start();
mic.Start();
}
}
void mic_BufferReady(object sender, EventArgs e) {
byte[] data = new byte[mic.GetSampleSizeInBytes(mic.BufferDuration)];
mic.GetData(data);
byteList.Add(data);
}
void t_Elapsed(object sender, ElapsedEventArgs e) {
mic.Stop();
timer.Stop();
byte[] buffer = new byte[byteList.Select(x => x.Length).Sum()];
int offset = 0;
foreach (var buff in byteList) {
System.Buffer.BlockCopy(buff, 0, buffer, offset, buff.Length);
offset += buff.Length;
}
using (FileStream fs = new FileStream(outPutFile, FileMode.Create)) {
WaveHeader.WriteHeader(fs, buffer.Length, 1, mic.SampleRate);
fs.Write(buffer, 0, buffer.Length);
}
}
}
class WaveHeader {
static byte[] RIFF_HEADER = new byte[] { 0x52, 0x49, 0x46, 0x46 };
static byte[] FORMAT_WAVE = new byte[] { 0x57, 0x41, 0x56, 0x45 };
static byte[] FORMAT_TAG = new byte[] { 0x66, 0x6d, 0x74, 0x20 };
static byte[] AUDIO_FORMAT = new byte[] { 0x01, 0x00 };
static byte[] SUBCHUNK_ID = new byte[] { 0x64, 0x61, 0x74, 0x61 };
private const int BYTES_PER_SAMPLE = 2;
public static void WriteHeader(System.IO.Stream targetStream,
int byteStreamSize, int channelCount, int sampleRate) {
int byteRate = sampleRate * channelCount * BYTES_PER_SAMPLE;
int blockAlign = channelCount * BYTES_PER_SAMPLE;
targetStream.Write(RIFF_HEADER, 0, RIFF_HEADER.Length);
targetStream.Write(PackageInt(byteStreamSize + 42, 4), 0, 4);
targetStream.Write(FORMAT_WAVE, 0, FORMAT_WAVE.Length);
targetStream.Write(FORMAT_TAG, 0, FORMAT_TAG.Length);
targetStream.Write(PackageInt(16, 4), 0, 4);
targetStream.Write(AUDIO_FORMAT, 0, AUDIO_FORMAT.Length);
targetStream.Write(PackageInt(channelCount, 2), 0, 2);
targetStream.Write(PackageInt(sampleRate, 4), 0, 4);
targetStream.Write(PackageInt(byteRate, 4), 0, 4);
targetStream.Write(PackageInt(blockAlign, 2), 0, 2);
targetStream.Write(PackageInt(BYTES_PER_SAMPLE * 8), 0, 2);
targetStream.Write(SUBCHUNK_ID, 0, SUBCHUNK_ID.Length);
targetStream.Write(PackageInt(byteStreamSize, 4), 0, 4);
}
static byte[] PackageInt(int source, int length = 2) {
if ((length != 2) && (length != 4))
throw new ArgumentException("length must be either 2 or 4", "length");
var retVal = new byte[length];
retVal[0] = (byte)(source & 0xFF);
retVal[1] = (byte)((source >> 8) & 0xFF);
if (length == 4) {
retVal[2] = (byte)((source >> 0x10) & 0xFF);
retVal[3] = (byte)((source >> 0x18) & 0xFF);
}
return retVal;
}
}