Using Speech with C# and Visual Studio 2013.
NOTE: I used this tutorial with Visual Studio 2017 and working well with the source code.
This example will show you how to use Speech with C# an Visual Studio 2013 to recognition your words, speak your words from your list and also command the exit application.
Create one empty project from File – New Project.
You have now one Form and you will add 3 buttons and one TextBox.
After that will need to select MultiLine by using right click on the arrow of TextBox.
The next image shows you these 3 buttons and TextBox.
You need to activate Speech from Visual Studio 2013 IDE:
Go to the right panel and Add Reference ( use right-click and select it – see next image).
Then select Speech and click Apply – see next image:
All buttons will have the source code, just double click on buttons and will have the functions for all buttons.
Also If you want to rename the buttons just search Properties and then Text and rename buttons or Form title.
I used Speak, Start and Stop for buttons.
I used also one if-else with exit word from the list, to stop the application.
The list will have all words to recognition your words from the list.
You need also to have hardware microphone or portable microphone and also you must activate it under windows.
Now, the source code of your form is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Speech.Synthesis; using System.Speech.Recognition; using System.Threading; namespace Speaking { public partial class Form1 : Form { public Form1() { InitializeComponent(); } SpeechSynthesizer sSynth = new SpeechSynthesizer(); PromptBuilder pBuikder = new PromptBuilder(); SpeechRecognitionEngine sRecognize = new SpeechRecognitionEngine(); private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { pBuikder.ClearContent(); pBuikder.AppendText(textBox1.Text); sSynth.Speak(pBuikder); } private void button2_Click(object sender, EventArgs e) { button2.Enabled = false; button3.Enabled = true; Choices sList = new Choices(); sList.Add(new String[]{"hello","hi","who","how","working","you","test","working","exit"}); Grammar gr = new Grammar(new GrammarBuilder(sList)); try { sRecognize.RequestRecognizerUpdate(); sRecognize.LoadGrammar(gr); sRecognize.SpeechRecognized += sRecognize_SpeakRecognized; sRecognize.SetInputToDefaultAudioDevice(); sRecognize.RecognizeAsync(RecognizeMode.Multiple); } catch { return; } } private void sRecognize_SpeakRecognized(object sender, SpeechRecognizedEventArgs e) { //MessageBox.Show("Speek " + e.Result.Text.ToString()); if (e.Result.Text == "exit") { Application.Exit(); } else { textBox1.Text = textBox1.Text + " " + e.Result.Text.ToString(); } } private void button3_Click(object sender, EventArgs e) { sRecognize.RecognizeAsyncStop(); button2.Enabled = true; button3.Enabled = false; } } } |
Using F7 Buil Solution and then Start Debugging – F5 you will run the application.