简体   繁体   English

如何在可执行的c#WinForms App中构造语音识别代码?

[英]How to structure speech recognition code in an executable c# WinForms App?

link to original code - please look at it first. 链接到原始代码-请先查看。

I'm not sure how to write this code so that it runs properly. 我不确定如何编写此代码以使其正常运行。 So far my attempt: 到目前为止,我的尝试:

The code in the website is using System Speech Recognition from Microsoft to record audio from microphone and turn it into text. 该网站中的代码使用Microsoft的系统语音识别功能来录制麦克风的音频并将其转换为文本。 Except, I don't know how to format that code on the website properly. 除非,我不知道如何在网站上正确格式化该代码。 The below certainly does not work. 下面肯定不起作用。 I get red underlines everywhere. 我到处都有红色下划线。 I'm also not sure how 'event-handler' code is supposed to look like. 我也不确定“事件处理程序”代码的外观。

using System;
using System.Collections.Generic;
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Speech.Recognition;

namespace SystemSpeechRecognition_winForms
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


    private void button2_Click(object sender, EventArgs e)
    {
        SpeechRecognitionEngine _speechRecognitionEngine = new SpeechRecognitionEngine();

        _speechRecognitionEngine.SetInputToDefaultAudioDevice();

        DictationGrammar _dictationGrammar = new DictationGrammar();

        _speechRecognitionEngine.LoadGrammar(_dictationGrammar);

        _speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);

ERROR for the four lines This is the error: . 四行错误这是错误:。 "delegate System.EventHandler" "Represents the method that will handle an event that has no event data" "Error: No overload for 'SpeechRecognized' matches delegate 'System.EventHanndler' “委托System.EventHandler”“代表将处理没有事件数据的事件的方法”“错误:“ SpeechRecognized”没有重载匹配委托“ System.EventHanndler”

        _speechRecognitionEngine.SpeechRecognized -= new EventHandler(SpeechRecognized);



        _speechRecognitionEngine.SpeechHypothesized -= new EventHandler(SpeechHypothesizing);


        _speechRecognitionEngine.SpeechRecognized += new EventHandler(SpeechRecognized);

        _speechRecognitionEngine.SpeechHypothesized += new EventHandler(SpeechHypothesizing);

    }

    private void SpeechHypothesizing(object sender, SpeechHypothesizedEventArgs e)
    {

        ///real-time results from the engine

        string realTimeResults = e.Result.Text;

    }


    private void SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {

        ///final answer from the engine

        string finalAnswer = e.Result.Text;

    }

}

您遇到了编译问题,因为-以其中一个事件为例SpeechRecognized事件的类型为EventHandler<SpeechRecognizedEventArgs>并且您尝试为其分配非通用EventHandler类的实例。

_speechRecognitionEngine.SpeechRecognized -= new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognized);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM