Audio Recorder in C#

Today I am going to show you how to create a program that records voice and plays it (microphone) using C#.

  1. Let’s start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your program as microphone or whatever you want.
  2. Next, add three buttons named Button1 as record button for recording voice, Button2 as save and stop button, and Button3 for playing the recorded audio. You must design your interface like this.

When design is finished move on to code

Import System.Runtime.InteropServices namespace

  1. using System.Diagnostics;  
  2.  using System;  
  3.  using System.Windows.Forms;  
  4.  using System.Collections;  
  5.  using System.Drawing;  
  6.  using Microsoft.VisualBasic;  
  7.  using System.Data;  
  8.  using System.Collections.Generic;  
  9.  using System.Runtime.InteropServices;  
Now, in your code module, create a function named record that will access the winmm.dll
  1. [DllImport("winmm.dll",EntryPoint="mciSendStringA", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]  
  2. private static extern int record(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);  
For record button for Button1, put this code below
  1. public void Button1_Click(System.Object sender, System.EventArgs e)  
  2. {  
  3.    timer1.Enabled = true;  
  4.    timer1.Start();  
  5.    record("open new Type waveaudio Alias recsound""", 0, 0);   
  6.    record("record recsound""", 0, 0);   
  7. }  
The function named record was called here to open a wav audio file that is named as recsound. Then this will record sound as you click the record button. Note: Provide headset with microphone or speaker in your PC or laptop for you to say the words to record.

For save and stop button for Button2, put this code below

  1. public void Button2_Click(System.Object sender, System.EventArgs e)  
  2. {  
  3.    timer1.Stop();  
  4.    timer1.Enabled = false;  
  5.    record("save recsound d:\\mic.wav""", 0, 0);  
  6.    record("close recsound""", 0, 0);  
  7. }  
The recsound alias that we initialized in the record button was called here. This button will save the recorded audio file and saved into D directory and will named as mic.wav. Then after saving, we close the recorded sound.

For play button for Button3, put this code below
  1. public void Button3_Click(System.Object sender, System.EventArgs e)  
  2. {  
  3.    ms = 0;  
  4.    h = 0;  
  5.    s = 0;  
  6.    m = 0;  
  7.    timer1.Enabled = false;  
  8.    lblhur.Text = "00";  
  9.    lblmin.Text = "00";  
  10.    lblsecond.Text = "00";  
  11.    (new Microsoft.VisualBasic.Devices.Audio()).Play("d:\\mic.wav");  
  12. }  
We used the Play function in Audio to play the saved file in D Directory, the mic.wav, that we recorded its sound earlier.Click first record, say the words you wanted to say, click the save button, and play it.