• We just launched and are currently in beta. Join us as we build and grow the community.

Record Voice and Play Recorded Sound in C#

Gabriel_Alert

Platform API Engineer
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
171
Likes
122
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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:

recdesign.png


3, 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

    System.Data

    ;
  7. using

    System.Collections.Generic

    ;


  8. using

    System.Runtime.InteropServices

    ;

4. 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)

    ;

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

    void

    Button1_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  2. {
  3. record(

    "open new Type waveaudio Alias recsound"

    , ""

    , 0

    , 0

    )

    ;

  4. record(

    "record recsound"

    , ""

    , 0

    , 0

    )

    ;
  5. }

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.

  1. public

    void

    Button2_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  2. {
  3. record(

    "save recsound c:\\

    mic.wav"

    , ""

    , 0

    , 0

    )

    ;

  4. record(

    "close recsound"

    , ""

    , 0

    , 0

    )

    ;
  5. }

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.
  1. public

    void

    Button3_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  2. {
  3. (

    new

    Microsoft.

    VisualBasic

    .

    Devices

    .

    Audio

    (

    )

    )

    .

    Play

    (

    "c:\\

    mic.wav"

    )

    ;
  4. }

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

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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top