Question

I tried implementing a tutorial for Microsoft Speech Recognition. I get no errors but still the voice is not recognized. The Code is like

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;  
using System.Windows.Forms;
using Microsoft.Speech.Recognition;

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
    //SpeechRecognizer recognizer;
    SpeechRecognitionEngine sre;

    public Form1()
    {
        InitializeComponent();
         sre = new SpeechRecognitionEngine();
         sre.SetInputToWaveFile(@"c:\Test\Colors.wav");

         Console.WriteLine("here");


         // Create a simple grammar that recognizes "red", "green", or "blue".
         Choices colors = new Choices();
         colors.Add(new string[] { "red", "green", "blue" });
         Console.WriteLine("here");

         // Create a GrammarBuilder object and append the Choices object.
         GrammarBuilder gb = new GrammarBuilder();
         gb.Append(colors);

         // Create the Grammar instance and load it into the speech recognition engine.
         Grammar g = new Grammar(gb);
         sre.LoadGrammar(g);

         // Register a handler for the SpeechRecognized event.

         // Start recognition.
         sre.SpeechRecognized +=
           new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);


        sre.Recognize();
         Console.WriteLine("here");

    }

    private void Form1_Load(object sender, EventArgs e)
    {


    }

    // Create a simple handler for the SpeechRecognized event.
    void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        Console.WriteLine("here");

        MessageBox.Show("Speech recognized: " + e.Result.Text);
    }

}
}

Kindly help me sort out !! I have no idea why it is not working and am new to C# and Visual Studio

PS: I also get messages in the output window like the following While running the program

The thread '<No Name>' (0x674) has exited with code 0 (0x0).
The thread '<No Name>' (0x1ee0) has exited with code 0 (0x0).
The thread '<No Name>' (0xf8) has exited with code 0 (0x0).
The thread '<No Name>' (0x760) has exited with code 0 (0x0).
The thread 'vshost.RunParkingWindow' (0x1184) has exited with code 0 (0x0).
Was it helpful?

Solution

Try this code works for me

     public partial class MainWindow : Window
        {

            SpeechRecognitionEngine _recognizer;
            SpeechSynthesizer sre = new SpeechSynthesizer();
            int count = 1;

            public MainWindow()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            try
            {
                var culture = new CultureInfo("en-US");
                _recognizer = new SpeechRecognitionEngine(culture);
                _recognizer.SetInputToDefaultAudioDevice();
                _recognizer.LoadGrammar(GetGrammer());
                _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);

                _recognizer.RecognizeAsync(RecognizeMode.Multiple);

                sre.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Child);
                sre.Rate = -2;

            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.InnerException.Message);
            }
        }

        private static Grammar GetGrammer()
        {

            var choices = new Choices();
            //add custom commands
            choices.Add(File.ReadAllLines(@"Commands.txt"));
            //to add the letters to the dictionary
            choices.Add(Enum.GetNames(typeof(Keys)).ToArray());

            var grammer = new Grammar(new GrammarBuilder(choices));

            return grammer;
        }

        void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {

            string speech = e.Result.Text; 

        //to type letters in open application like notepad
       if (Enum.GetNames(typeof(Keys)).Contains(speech))
        {
            try
            {   //send the string to the application
                SendKeys.SendWait("{" + speech + "}");
            }
            catch (ArgumentException)
            {

            }            
        }  

        //handle custom commands
            switch (speech)
            {

                case "Hello":
                    sre.Speak("Goodmorning ");
                    break;

                case "Notepad":
                    System.Diagnostics.Process.Start("Notepad");
                    break;
                case "Maximize":
                    this.WindowState = System.Windows.WindowState.Maximized;
                    break;
                case "Minimize":
                    this.WindowState = System.Windows.WindowState.Minimized;
                    break;
                case "Restore":
                    this.WindowState = System.Windows.WindowState.Normal;
                    break;
                case "Close":
                    Close();
                    break;
            }
        }       
    }

You would also need to create a .txt file to load the grammer with the commands each in single line like below

Notepad
Close
Minimize
Maximize
Open
Hello
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top