首页 > 解决方案 > C#语音识别不会回答我

问题描述

我做了这个简单的代码,但它似乎无法识别我的声音。我尝试了从使用另一个输入(麦克风)到读取异常的所有方法。它总是直接说“出了点问题”。有人可以检查我的代码是否有错误吗?或者如果你有同样的问题,我该如何解决?我也在互联网上搜索了一段时间,但我找不到正确的答案。

代码:

using System;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Threading;

namespace voice_bot
{
    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));
            s.Speak("hello, my name is voice bot. If you would like to talk with me, go ahead");

            try
            {
                rec.RequestRecognizerUpdate();
                rec.LoadGrammar(gr);
                rec.SpeechRecognized += rec_SpeachRecognized;
                rec.SetInputToDefaultAudioDevice();
                rec.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch (Exception e)
            {
                s.Speak("Something went wrong");    
            }          

        }

        public void say(String h)
        {
            s.Speak(h);
        }





        private void rec_SpeachRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            string r = e.Result.Text;


            if (r == "hello")
            {            
                s.Speak("hi");                
            }

            if (r == "how are you")
            {
                s.Speak("good, how are you?");               
            }
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

标签: c#voicespeech

解决方案


我不确定你在什么平台上尝试这个,但System.Speech它已经很老了。

你可以试试微软最新的语音平台(注意:我在 System.Speech 和这个最新的语音平台上都工作过)。您可以在此处查看新平台:https ://aka.ms/speech/sdk 。有一堆示例应该可以很快引导您找到一个可行的解决方案,它适用于“所有”现代平台操作系统(windows、linux、android、ios、mac 等)并通过“所有”现代编程语言(c++、c#、java、javascript、python、objective-c、swift等...)

--抢劫


推荐阅读