Record Sound from Microsound

If you are programmer than then you might be interested in building such applications which can access some of the hardware or the software drivers of the system. And if you want build a Sound recorder or Spy software then you need to record the sound by accessing the mic-in or microphone.

Following shows the demo code in C# .NET to record sound in real time from Mic-in. The code is tested on .NET framework 2.0 and above.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace recordsound
{
class Program
{
[DllImport("winmm.dll")]
private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr oCallback);

static void Main(string[] args)
{
mciSendString("Open new type waveaudio alias RecWavFile", null, 0, IntPtr.Zero); // "RecWavFile" is the alias name. you can provide any valid name that u want
mciSendString("Record RecWavFile", null, 0, IntPtr.Zero);

System.Threading.Thread.Sleep(60000); // Record for 60 seconds as it delays the thread
mciSendString("Save RecWavFile newrecord.wav", null, 0, IntPtr.Zero); // Record tream is saved as "newrecord.wav"
mciSendString("Close RecWavFile", null, 0, IntPtr.Zero);
}
}
}
To execute more specfic function you can use :-
/* To pause the recording */
mciSendString("pause RecWavFile", null, 0, IntPtr.Zero);

/* To Resume recoding */

mciSendString("resume RecWavFile", null, 0, IntPtr.Zero);

To execute even more commands visit this :-

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *