Voice Recognition - Visual Studio
Let us study
voice recognition using visual studio.
- Open visual studio > New Project > Visual C# > Windows Form Application > OK
- Now a new project is started
- Go to solution explorer > right click reference > add reference > in .NET tab select “System.Speech” > OK
- Now Design the form as shown in the picture and name it as Below
- Right click on the form design > view code > copy the code as shown in video below.
Source code :-
using System;
using System.Windows.Forms;
using System.Speech;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Diagnostics;
namespace voice_recognition // your project name. be care while copying.
{
public partial class Form1 : Form
{
SpeechSynthesizer speechsynth = new SpeechSynthesizer();
SpeechRecognitionEngine receng = new SpeechRecognitionEngine();
Choices choice = new
Choices();
public Form1()
{
InitializeComponent();
}
private void
startbtn_Click(object sender, EventArgs e)
{
startbtn.Enabled = false;
stopbtn.Enabled = true;
choice.Add(new string[]
{ "hello", "how are you", "what
is the current time", "open
blog", "thank you", "close" });
Grammar gr = new Grammar(new GrammarBuilder(choice));
try
{
receng.RequestRecognizerUpdate();
receng.LoadGrammar(gr);
receng.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(receng_SpeechRecognized);
receng.SetInputToDefaultAudioDevice();
receng.RecognizeAsync(RecognizeMode.Multiple);
}
catch (Exception
ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
void receng_SpeechRecognized(object
sender, SpeechRecognizedEventArgs e)
{
switch (e.Result.Text.ToString())
{
case "hello":
speechsynth.SpeakAsync("hai user");
break;
case "how
are you":
speechsynth.SpeakAsync("iam fine. what about you");
break;
case "what
is the current time":
speechsynth.SpeakAsync("right now it is
" + DateTime.Now.ToLongTimeString());
break;
case "thank
you":
speechsynth.SpeakAsync("well. same to you");
break;
case "open
blog":
Process.Start("chrome", "http://homotrick.blogspot.in");
break;
case "close":
speechsynth.Speak("see you later. bye
bye");
Application.Exit();
break;
}
list1.Items.Add(e.Result.Text.ToString());
}
private void
stopbtn_Click(object sender, EventArgs e)
{
receng.RecognizeAsyncStop();
startbtn.Enabled = true;
stopbtn.Enabled = false;
}
}
}
Video :-