0

嗨,我这里有情况,我不知道如何解决这个问题。这段代码很好,有 0 个错误,但每当我执行 F5 时,它都会向我显示一个没有语音且无法识别的空白表格,请帮忙。请我真的需要帮助有人可以帮助我。
现在这一行仅用于“看起来您的帖子主要是代码;请添加更多详细信息”;

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.Synthesis;
using System.Speech.Recognition;
using System.Threading;

namespace a.i
{
    public partial class Form1 : Form
    {
        SpeechSynthesizer s = new SpeechSynthesizer();
        Choices list = new Choices();
        public Form1()
        {

            SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
            list.Add(new String[] { "hello", "how are you" });

            Grammar gr = new Grammar(new GrammarBuilder(list));

            try
            {
                rec.RequestRecognizerUpdate();
                rec.LoadGrammar(gr);
                rec.SpeechRecognized += rec_SpeachRecognized;
                rec.SetInputToDefaultAudioDevice();
                rec.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch { return; }
            s.SelectVoiceByHints(VoiceGender.Neutral);

            s.Speak("Hello, my name is Gabriel ChatterBot");

            InitializeComponent();
        }

        public void say(string h)
        {
            s.Speak(h);
        }
        private void rec_SpeachRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            String r = e.Result.Text;
            //what you say
            if (r == "hello")
            {
                // what it says
                say("hi");
            }
            //what you say
            if (r == "how are you")
            {
                // what it says
                say("Great, and you!");
            }
        }
    }
}
4

1 回答 1

1

从您发布的异常来看,我认为您没有正确初始化语法和 SpeechRecognitionEngine。看来您需要为它指定一种语言/文化。来自文档: https ://msdn.microsoft.com/en-us/library/hh378426(v=office.14).aspx

// Create a new SpeechRecognitionEngine instance.
SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
于 2017-01-14T23:47:51.867 回答