语音识别未返回结果(WindowsFormsApp3.exe信息:0:SAPI无法实现语音字母选择。)

时间:2018-08-21 16:22:38

标签: c# voice-recognition

因此,我正在新学习编码,并且遵循了一个教程(https://www.youtube.com/watch?v=AB9lfHDOe5U)教我如何使用语音识别。但是,当我尝试它时,没有收到任何错误消息,但是没有任何响应,好像它没有拿起我的麦克风。我已经确认我的麦克风没有问题,并且我尝试对代码进行一些修复,但没有结果。一些帮助将不胜感激:)

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 WindowsFormsApp3
{
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();
        commands.Add(new string[] { "say hello", "print my name" });
        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 "say hello":
                MessageBox.Show("Hello Master.");
                break;
            case "print my name":
                richTextBox1.Text += "\nToby";
                break;


        }
    }

    private void btnDisable_Click(object sender, EventArgs e)
    {
        recEngine.RecognizeAsyncStop();
        btnDisable.Enabled = false;
    }
}

}

0 个答案:

没有答案