在进行识别之前必须至少加载一个语法。 [英] At least one grammar must be loaded before doing a recognition.

查看:177
本文介绍了在进行识别之前必须至少加载一个语法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的代码遇到了这个问题..

程序:Visual Studio 2017 代码语言:C#

当我尝试激活我的代码时,我不断收到此弹出错误消息:



System.InvalidOperationException:'在执行操作之前必须至少加载一个语法一个认可。



它选择这部分代码:



recEngine.RecognizeAsync(RecognizeMode。多个);





整个代码;

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





如果你知道如何解决这个问题我会非常快乐的原因这是一个我真正想要工作的代码,它也是我的第一个项目之一,我真的开始进入编码,但是当你遇到这样的问题时,它真的很无聊!



我尝试了什么:



我试过以下解决方案



c# - 在进行识别之前必须至少加载一个语法 - Stack Overflow [ ^ ]



必须加载至少一个语法错误c# [ <一个href =https://www.codeproject.com/Questions/1080350/At-least-one-grammar-must-be-loaded-error-csharp\"target =_ blanktitle =New Window> ^ ]



虽然它们都没有工作



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

推荐答案

我建​​议使用LoadGrammer(而不是 async):



SpeechRecognitionEngine.LoadGrammar Method(Grammar)(System.Speech.Recognition) [ ^ ]
I would suggest using LoadGrammer (instead of "async"):

SpeechRecognitionEngine.LoadGrammar Method (Grammar) (System.Speech.Recognition)[^]


这篇关于在进行识别之前必须至少加载一个语法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆