user3755903
user3755903

Reputation: 3

Speech recognition not working

I am developing a WPF application which uses speech recognition. The events does not fire up when the grammar words are spoken. Secondly, I am not sure whether the engine starts up on not. How to check that? Following is the code.

namespace Summerproject_trial
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
       private SpeechRecognitionEngine recEngine = 
                                    new SpeechRecognitionEngine();           

        public MainWindow()
        {
            InitializeComponent();
            Choices mychoices = new Choices();
            mychoices.Add(new string[] {"Ok", "Test", "Hello"});
            GrammarBuilder gb = new GrammarBuilder();
            gb.Append(mychoices);
            Grammar mygrammar = new Grammar(gb);
            recEngine.LoadGrammarAsync(mygrammar);          

            recEngine.SpeechRecognized += 
                               new EventHandler<SpeechRecognizedEventArgs>
                                              (recEngine_SpeechRecognized);

            recEngine.SetInputToDefaultAudioDevice();              
        }

        void recEngine_SpeechRecognized(object sender,
                                        SpeechRecognizedEventArgs e)
        {
            MessageBox.Show("You said: " + e.Result.Text);
        }    
    }
}

Upvotes: 0

Views: 1923

Answers (2)

Eric Brown
Eric Brown

Reputation: 13932

@Anri's answer is needed, but you also need to create the SpeechRecognitionEngine with a CultureInfo. (You can create a SpeechRecognitionEngine without a CultureInfo, but then you need to set the recognizer language explicitly.)

Also: Mobile earphones (by which I assume you mean some sort of Bluetooth headset) will typically NOT work with System.Speech. The SR engine used in the desktop SR engine requires higher quality audio input than it can get from Bluetooth.

So, complete code that should work:

   private SpeechRecognitionEngine recEngine = 
                                new SpeechRecognitionEngine("en-US");           

    public MainWindow()
    {
        InitializeComponent();
        Choices mychoices = new Choices();
        mychoices.Add(new string[] {"Ok", "Test", "Hello"});
        GrammarBuilder gb = new GrammarBuilder();
        gb.Append(mychoices);
        Grammar mygrammar = new Grammar(gb);
        recEngine.LoadGrammarAsync(mygrammar);          

        recEngine.SpeechRecognized += 
                           new EventHandler<SpeechRecognizedEventArgs>
                                          (recEngine_SpeechRecognized);

        recEngine.SetInputToDefaultAudioDevice();
        recEngine.RecognizeAsync(RecognizeMode.Multiple);
    }

Upvotes: 0

Anri
Anri

Reputation: 6265

You forgot to start listening to input.

Try this in the end of your constructor.

recEngine.RecognizeAsync(RecognizeMode.Multiple);

Upvotes: 1

Related Questions