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.
OUTPUT
Conclusion
In case of any doubts / errors, please drop in a comment.
See Also
Your are great man ;)
ReplyDelete