Click here to Skip to main content
15,886,562 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
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 System.Speech.Recognition;
 
namespace re
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            SpeechRecognitionEngine sp = new SpeechRecognitionEngine();
            InitializeComponent();
        }
 

        private SpeechRecognitionEngine LoadDictationGrammars()
        {
 
            // Create a default dictation grammar.
            DictationGrammar defaultDictationGrammar = new DictationGrammar();
            defaultDictationGrammar.Name = "default dictation";
            defaultDictationGrammar.Enabled = true;
 
            // Create the spelling dictation grammar.
            DictationGrammar spellingDictationGrammar =
              new DictationGrammar("grammar:dictation#spelling");
            spellingDictationGrammar.Name = "spelling dictation";
            spellingDictationGrammar.Enabled = true;
 
            // Create the question dictation grammar.
            DictationGrammar customDictationGrammar =
              new DictationGrammar("grammar:dictation");
            customDictationGrammar.Name = "question dictation";
            customDictationGrammar.Enabled = true;
 
            // Create a SpeechRecognitionEngine object and add the grammars to it.
            SpeechRecognitionEngine recoEngine = new SpeechRecognitionEngine();
            recoEngine.LoadGrammar(defaultDictationGrammar);
            recoEngine.LoadGrammar(spellingDictationGrammar);
            recoEngine.LoadGrammar(customDictationGrammar);
 
            // Add a context to customDictationGrammar.
            customDictationGrammar.SetDictationContext("How do you", null);
 
            recoEngine.BabbleTimeout = TimeSpan.FromSeconds(10.0);
            recoEngine.EndSilenceTimeout = TimeSpan.FromSeconds(10.0);
            recoEngine.EndSilenceTimeoutAmbiguous = TimeSpan.FromSeconds(10.0);
            recoEngine.InitialSilenceTimeout = TimeSpan.FromSeconds(10.0);
 
            recoEngine.SpeechRecognized += RecognizerSpeechRecognized;
            // recoEngine.RecognizeCompleted += RecognizerRecognizeCompleted
            recoEngine.SetInputToDefaultAudioDevice();
            recoEngine.RecognizeAsync(RecognizeMode.Multiple);
 
            return recoEngine;
 
        }
        private void RecognizerSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result != null && e.Result.Text =="grammarDictation")
            {
                richTextBox1.Text = e.Result.Text;
            }
            else
            {
                richTextBox1.Text = "Recognized text not available.";
            }
        }
 
        private void form1_Load(object sender, EventArgs e)
        {
 
        }
 

    }
}
Posted
Updated 13-Mar-13 2:58am
v2
Comments
bbirajdar 13-Mar-13 8:59am    
any errors?
Pheonyx 13-Mar-13 8:59am    
Are you getting an error?
Firstly, I would change the order of code in your constructor to be:

InitializeComponent();
SpeechRecognitionEngine sp = new SpeechRecognitionEngine();

instead of what you have.
[no name] 13-Mar-13 8:59am    
What does "it does not work properly" even mean?
Andy411 13-Mar-13 9:03am    
What is not running? All I see on first look is, that you have a SpeechRecognitionEngine sp in the ctor and you're not using it.

Where do you call the LoadDictationGrammars method?
ZurdoDev 13-Mar-13 11:47am    
What is happening?

1 solution

I have no idea what is wrong with your code, but luckily I have made a similar program an hour ago. This code will work, maybe you should add some adjustments though:
C#
SpeechRecognitionEngine Engine = new SpeechRecognitionEngine();
DictationGrammar Grammar = new DictationGrammar();

private void frmManager_Load(object sender, EventArgs e)
{
    /* Set to the default device. */
    Engine.SetInputToDefaultAudioDevice();

    /* Enable Dictation Grammar. */
    Grammar.Name = "Default Dictation";
    Grammar.Enabled = true;

    /* Add the Grammar. */
    Engine.LoadGrammar(Grammar);

    /* Handle Events. */
    Engine.SpeechRecognized += new EventHandler<speechrecognizedeventargs>(Engine_SpeechRecognized);

    /* Do speak. */
    Engine.RecognizeAsync(RecognizeMode.Multiple);

}

void Engine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    string RecognizedText = e.Result.Text;

    /*richTextBox.Text = RecognizedText ?*/

}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 27-Mar-13 1:00am    
Voted 4. You see, when I see a good answer, I can up-vote it. :-)
One little problem of the code is bad naming of methods with violation of (good) Microsoft naming convention. Auto-generated names are never good for using in the code, should be always renamed to something semantic. That's why we have a refactorization engine.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900