Record Voice and Play Recorded Sound in C#
Submitted by donbermoy on Tuesday, July 1, 2014 - 23:25.
Language
Today in C#, i will teach you how to create a program that records voice and plays it (microphone) using C#.
Now, let's start this tutorial!
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.
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:
3, Import System.Runtime.InteropServices namespace.
4. Now, in your code module, create a function named record that will access the winmm.dll.
5.For record button for Button1, put this code below.
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.
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.
We used the Play function in Audio to play the saved file in C 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. :)
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
- 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;
- [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);
- public void Button1_Click(System.Object sender, System.EventArgs e)
- {
- record("open new Type waveaudio Alias recsound", "", 0, 0);
- record("record recsound", "", 0, 0);
- }
- public void Button2_Click(System.Object sender, System.EventArgs e)
- {
- record("save recsound c:\\mic.wav", "", 0, 0);
- record("close recsound", "", 0, 0);
- }
- public void Button3_Click(System.Object sender, System.EventArgs e)
- {
- }
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Comments
How to change the samplespersec using the same record function
I have used the same function and able to record and stop,
I want to change the samples per sec to 44100. but it doesnt reflect.
can you please help me with that ?
Add new comment
- Add new comment
- 1424 views