Record Voice Sound In C#

Today in C#, I will show you how to make a program that records voice and plays it (receiver) utilizing C#.

Presently, how about we start this instructional exercise!

1. How about we start with making a Windows Form Application for this instructional exercise by following the accompanying strides in Microsoft Visual Studio: Go to File, click New Project, and pick Windows Application and name your program as microphone.

2. Next, include three catches named Button1 as record button for recording voice, Button2 as spare and stop catch, and Button3 for playing the recorded sound. You should plan your interface like this:

record voice in C#

3, Import System.Runtime.InteropServices namespace.

using System.Diagnostics;
using System;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using System.Data;
using System.Collections.Generic;
using System.Runtime.InteropServices;


4. Now, in your code module, create a function named record that will access the winmm.dll.

[DllImport("winmm.dll",EntryPoint="mciSendStringA", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
 private static extern int record(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);


5.For record button for Button1, put this code below.

public void Button1_Click(System.Object sender, System.EventArgs e)
           record("open new Type waveaudio Alias recsound", "", 0, 0);

           record("record recsound", "", 0, 0);


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.

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

public void Button2_Click(System.Object sender, System.EventArgs e)
        record("save recsound c:\mic.wav", "", 0, 0);
        record("close recsound", "", 0, 0);


The recsound alias that we initialized in the record button was called here. This button will save the recorded audio file and saved into C directory and will named as mic.wav. Then after saving, we close the recorded sound.

7.For play button for Button3, put this code below.

public void Button3_Click(System.Object sender, System.EventArgs e)
            (new Microsoft.VisualBasic.Devices.Audio()).Play("c:\mic.wav");


We utilized the Play function in Audio to play the saved file in C Directory, the mic.wav, that we recorded its sound earlier.

Click the first record, say the words you needed to say, click the save button, and execute it.

Sharing is caring!

Facebook Comments

Leave a Reply