Using Voice Commands with C#
Using Voice Commands with C#
You can take advantage of the voice command feature in your projects with C#.To be more specific, you can make your program work and perform its features by voice instead of typing with C#.
Let's get to how we will do this. You need to follow the steps below one by one.
Using the Library
From the menu, follow "Project > Add Reference > FrameWork" and add the "System.Speech" library to your project. Then, write the code below.using System.Speech.Recognition;
Object Definition and Settings
We will define the objects we are going to use and ensure events such as form_load happen.SpeechRecognitionEngine speechEngine = new SpeechRecognitionEngine();
private void Form1_Load(object sender, System.EventArgs e)
{
// use the default audio input device
speechEngine.SetInputToDefaultAudioDevice();
// specify the group of words to be recognized
Choices choises = new Choices("yes", "no");
// turn the words we defined into grammar
GrammarBuilder grammarBuilder = new GrammarBuilder(choises);
// create a new grammar object
Grammar grammar = new Grammar(grammarBuilder);
// load the created grammar into the speechEngine
speechEngine.LoadGrammar(grammar);
// when one of the specified words is recognized
speechEngine.SpeechRecognized += speechEngine_Event;
// let the speechEngine recognize words asynchronously and multiply
speechEngine.RecognizeAsync(RecognizeMode.Multiple);
}Voice Recognition
The process of detecting the user's voice and performing actions by voice in the project we wrote.void speechEngine_Event(object sender, SpeechRecognizedEventArgs e)
{
// among the recognized words
foreach (RecognizedWordUnit words in e.Result.Words)
{
// process according to the word
if (words.Text.Equals("yes"))
{
MessageBox.Show("Yes!");
}
else if (words.Text.Equals("no"))
{
MessageBox.Show("No!");
}
else
{
MessageBox.Show("The spoken word could not be recognized!");
}
}
}Errors and Solutions
If you receive an error such as No recognizer installed or an undefined language, it is because your system language is Turkish and the English language packs are not installed.You can get rid of these errors by setting your operating system language to English.


Yorum Gönder