How To Make A Sound Recorder In C#

sound recorder

hey ,today i will let you know how to create a sound recorder in c# by the way this is just a simple tutorial but this will let you know about the recording so you can make a  advanced recording application ok now first create a new project and name it and you need three buttons and one label so drag and and drop them from tool box into your form and now change the text of the each button as below

Button 1 - Record
Button2 - Stop And Save
Button3 - Play

now just keep the label 1 text empty now just double click your form and erase everything and add the whole code there

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

//Author : Mohamed Shimran
//Blog : http://www.ultimateprogrammingtutorials.blogspot.com

namespace soundrecorder
{
    public partial class Form1 : Form
    {
        [DllImport("winmm.dll")]
        private static extern int mciSendString(string MciComando, string MciRetorno, int MciRetornoLeng, int CallBack);

        string musica = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Visible = true;
            mciSendString("open new type waveaudio alias Som", null, 0, 0);
            mciSendString("record Som", null, 0, 0);
        
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label1.Visible = false;
            mciSendString("pause Som", null, 0, 0);

          

            SaveFileDialog save = new SaveFileDialog();

            save.Filter = "wave|*.wav";


            if (save.ShowDialog() == DialogResult.OK)
            {

                mciSendString("save Som " + save.FileName, null, 0, 0);
                mciSendString("close Som", null, 0, 0);
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (musica == "")
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Wave|*.wav";
                if (open.ShowDialog() == DialogResult.OK) { musica = open.FileName; }
            }
            mciSendString("play " + musica, null, 0, 0);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Visible = false;
        }


    }
}


now debug and enjoy .

23 comments

thanks Alot ... very good job done ...
simple and clear code
i have been searching for it for long
thanks alot

Reply

can you plz tell me how can we give folder name for saving the file in the code instead of opening dialogue box???

Reply

Hello Bro :)

go to button 2 codes and remove everything and put these

label1.Visible = false;
mciSendString("pause Som", null, 0, 0);

mciSendString("save Som D:/rec.wav", null, 0, 0);
mciSendString("close Som", null, 0, 0);

now when you click button 2 (stop and save) the recorded sound file will be saved in D:/ drive as rec.wav you can also change the drive (ie:C:/) and you can also save in a folder in D:/ "mciSendString("save Som D:/records/rec.wav", null, 0, 0);"

thanks :-bd

Reply

can you tell me that how to set the sampling rate to 16000, data type to 8 and channel to 2?

Reply

hey :)

this is not a advanced sound recorder also i am not much involved in audio , i am so sorry i have no idea about the sampling rate and data type to 8 and channel to 2 :( by the way you can get help from stackoverflow i guess so

Reply

hii.. i have saved the voice using c#and stored it in database.using sql 2005. .. can i get the code of retrieving back when i click on the button play.. plzzz help me..

Reply

hi , for that you need to use something with the JDBC connection.

Reply

Unable to load DLL 'winmm.dll': The specified procedure could not be found. (Exception from HRESULT: 0x8007007F)"}

Reply

While Debugging the program i am getting the following error:
Unable to load DLL 'winmm.dll': The specified procedure could not be found. (Exception from HRESULT: 0x8007007F)"}
So,please help me as early as possible......................

Reply

Take a look at this article http://pcsupport.about.com/od/findbyerrormessage/a/winmm-dll-not-found-missing-error.htm

Reply

Thank You.

Exactly what i needed for. Much better than MSDN page :)

Reply

Error 1 The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?)

Help me

Reply

Did you forget to add any namespaces in the tutorial?

Reply

Thanks!

Reply

Really nice. Is it possible to record anything that comes out of the speakers and not the mic? How would I change that?

Reply

Nice and simple code. Do you have any idea how to record skipping the silence?

Reply

Thanks alot Sir!! Took a part of our burden away!!!

Reply

Thanks it's working great.

Reply

Hello Dude..

I Just run the Code, Code was not Run. Error Was Occured. Is there any Include Special Class in this Project? could you tell me Exact Code.

Reply

how to add namespace in this Project?

Reply

getting "Can't find PInvoke DLL 'winmm.dll'." when i am trying to use Above code...
What can I do to resolve it ?

Reply

Post a Comment

Note: Only a member of this blog may post a comment.