Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I got this problem with my code..
Program: Visual Studio 2017 Code Language: C#
I keep getting this pop-up error message when I try to activate my code:

"System.InvalidOperationException: 'At least one grammar must be loaded before doing a recognition."

it selects this part of the code:

recEngine.RecognizeAsync(RecognizeMode.Multiple);


Whole code;
C#
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.Recognition;

namespace Voice_Recognition
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();

        public Form1()
        {
            InitializeComponent();
        }

        private void btnEnable_Click(object sender, EventArgs e)
        {
            recEngine.RecognizeAsync(RecognizeMode.Multiple);
            btnDisable.Enabled = true; 
        }
            private void Form1_Load(object sender, EventArgs e)
            {

            Choices commands = new Choices();
            string[] phrases = new string[] {
            "test", "chicken"
            };
            Choices choiceSet = new Choices(phrases);
            GrammarBuilder grammarBuilder = new GrammarBuilder(choiceSet);
            GrammarBuilder gBuilder = new GrammarBuilder();
            gBuilder.Append(commands);
            Grammar grammar = new Grammar(gBuilder);

            recEngine.LoadGrammarAsync(grammar);
            recEngine.SetInputToDefaultAudioDevice();
            recEngine.SpeechRecognized += RecEngine_SpeechRecognized;
            }

        private void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            switch (e.Result.Text)
            {
                case "balance breaker":
                    MessageBox.Show("Welsh Dragon, Balance Breaker");
                    break;
                case "balance break":
                    richTextBox1.Text += "\nBalance Break";
                    break;
            }
        }

        private void btnDisable_Click(object sender, EventArgs e)
        {
            recEngine.RecognizeAsyncStop();
            btnDisable.Enabled = false;
        }
    }
}


If you know how to solve this problem I would be very happy cause this is a code that I really want to work and It's also one of my first projects and I'm really starting to get in to coding, though when you get stuck like this, it gets really boring!

What I have tried:

I've tried the following "solutions"

c# - at least one grammar must be loaded before doing a recognition - Stack Overflow[^]

At least one grammar must be loaded error c#[^]

Though none of them worked
Posted
Updated 22-Jul-18 22:01pm

1 solution

I would suggest using LoadGrammer (instead of "async"):

SpeechRecognitionEngine.LoadGrammar Method (Grammar) (System.Speech.Recognition)[^]
 
Share this answer
 
Comments
Member 13921733 23-Jul-18 6:44am    
I am very new to this so I would appreciate if you could show me the exact code that I have to use.

Where am I supposed to put the code?

private void btnEnable_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsync(RecognizeMode.Multiple); Am I supposed to replace this code with SpeechRecognitionEngine.LoadGrammar Method (Grammar) (System.Speech.Recognition)??

btnDisable.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{

Choices commands = new Choices();
string[] phrases = new string[] {
"balance breaker", "balance break"
};
[no name] 25-Jul-18 15:53pm    
You had this:

Grammar grammar = new Grammar(gBuilder);

recEngine.LoadGrammarAsync(grammar);
recEngine.SetInputToDefaultAudioDevice();
recEngine.SpeechRecognized += RecEngine_SpeechRecognized;

Change the "LoadGrammarAsync" above to "LoadGrammer".

At least read your own code.

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