使用speech.recognition在C#中的空白表单

时间:2017-01-13 20:51:58

标签: c# system.speech.recognition

嗨,我有这种情况,我不知道如何解决这个问题。这个代码很好,有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!");
            }
        }
    }
}

1 个答案:

答案 0 :(得分: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"));