System.Speech在调试时无法识别语音

时间:2017-02-21 17:20:21

标签: c# visual-studio visual-studio-2015 speech-recognition speech-synthesis

我打算在C#中创建一个识别语音的表单应用程序然后用户可以说一些简单的命令,如" hi"并且计算机会响应"您好,我可以帮助您解决问题吗?我100%确定我的麦克风工作,但是当我调试程序时,它似乎无法识别麦克风中的任何命令或音频。我正在使用Visual Studio Community 2015.我想知道,有什么我错过或做错了吗?计算机加载表格并与我交谈,但它不能识别我的声音。我将不胜感激任何帮助。谢谢!

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;

namespace VoiceHost
{
    public partial class Form1 : Form
    {
        SpeechSynthesizer Host = new SpeechSynthesizer();
        Choices services = new Choices();

        public Form1()
        {


            SpeechRecognitionEngine UserRecog = new SpeechRecognitionEngine();

            services.Add(new string[] {"hi"});

            Grammar gr = new Grammar(new GrammarBuilder(services));
            Say("hello, my name is voice host, how can I help");

            try
            {
                UserRecog.RequestRecognizerUpdate();
                UserRecog.LoadGrammar(gr);
                UserRecog.SpeechRecognized += UserRecog_SpeechRecognized;
                UserRecog.SetInputToDefaultAudioDevice();
                UserRecog.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch { return; }
        }
        public void Say(string responseMessage)
        {
            Host.Speak(responseMessage);
        }

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

            if (userOrder == "hi")
            {
                Say("hi there, can I help you with someting");
            }
            InitializeComponent();

        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }

}

1 个答案:

答案 0 :(得分:-3)

catch {Exception ex} 这样你就可以看到代码中是否有任何中断。