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;
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