Pages

Tuesday 27 May 2014

Sound Recorder in C#



Hello Programmer,

Here you are going to create a Sound Recorder in C#. The Recorded sound will be saved in .wav format. So let's start;



Steps

1. Open Visual Studio -> New Project -> Windows Form Application with name
    SoundRecorder.

2. Now add 3 buttons and a  label to the empty form and design it as shown in 
    the below screenshot.



3. Now change the name and text of the added controls from the Properties dialog box;

   Control          Control Name                Text
    Button1           btnRecord                    Record
    Button2           btnStopandSave            Stop and Save
    Button3           btnPlay                        Play
    label1              label1                          Recording...

4. Now double click on the Record button and copy the below code;

            label1.Visible = true;
            mciSendString("open new type waveaudio alias Som", null, 0, 0);
            mciSendString("record Som", null, 0, 0);

5. Now double click on the Stop and Save button and copy the below code;

            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);
            }
         

6. Now double click on the  Play button and copy the below code;

           if (record == "")
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Wave|*.wav";
                if (open.ShowDialog() == DialogResult.OK) { record = open.FileName; }
            }
            mciSendString("play " + record, null, 0, 0);

7. Now double click on the  Form and copy the below code;

         label1.Visible = false;

8. Now please update your code from the complete code shown below;

Form1.cs


using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

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

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

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

        }

        private void btnRecord_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 btnStopandSave_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 btnPlay_Click(object sender, EventArgs e)
        {
            if (record == "")
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Wave|*.wav";
                if (open.ShowDialog() == DialogResult.OK) { record = open.FileName; }
            }
            mciSendString("play " + record, null, 0, 0);
        }
    }
}



8. Rebuild and Run the project;
  • Click on Record to start Recording.   
  • Recording label will be visible while sound is being recorded.
  • Click on Stop and Save button to Save the Recording. You will be asked for the location and filename to Save.
  • Now click on Play button to play the recorded file. You will have to select the file from the saved location.
        ENJOY Recording :-) 


OUTPUT




Conclusion

In case of any doubts / errors, please drop in a comment.


See Also

Thursday 22 May 2014

Text to Speech in C#


Hello Programmers,



Here you are gonna learn how to convert Text to Speech in C#  i.e you are going to create a software that Talks!

So lets start;






Thursday 15 May 2014

How to Start and Kill a Process in C#



Hi, 

Here you are gonna learn to Start and Kill a Process in C#. Suppose if you want to open / close an application such as Notepad, Calculator etc with C# codes, this tutorial will be helpful. 

Let's start;


Tuesday 6 May 2014

File Read and Write in C#


Hi,

Here you are gonna learn File Read and Write in C#.

The below project deals with;
    a) Getting data (2 names) from the
        user.
    b) Write the data into a text file.
    c) Reading the data from the text file
        and displaying it.

so let's start

Sunday 4 May 2014

How to Hide Folders in C#


Hi,


Hope you have read my previous post on Lock a Folder in C#. Now here you can learn How to Hide a Folder in C#. So let's start;

Wednesday 30 April 2014

Serialize and Deserialize in C#


Hi,


Here you are gonna learn about Serialization & Deserialization in C#.

Definition

Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called Deserialization (From MSDN).

Thursday 24 April 2014

Lock a Folder in C#


Hi,

Here you will learn how to lock and Unlock folders using C#. This tutorial is of good use if you are planning to create a Folder Locking Software. 


Let's start;

 

About Me

I am Tijo Tom, a Computer Engineer from India.Right from my schooling, I had a passion for Programming, PC Tricks, Virus Creation and Hacking.

I started this blog on Jan 1, 2014 to share my views and ideas with the world.

You can know more about me on