winforms SpeechRecognitionEngine

时间:2014-03-11 17:28:46

标签: c# .net winforms speech-recognition

我们已经开始使用speechRecognitionEngine,并基于我们在堆栈溢出时发现的应用程序构建了一个非常基本的应用程序。代码如下: -

public partial class Form1 : Form
{
    SpeechRecognitionEngine sr = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")); 

    public Form1()
    {
        InitializeComponent();
        // Create an in-process speech recognizer for the en-US locale.

    }

    private void BeginSpeach()
    {
        //Create grammar
        Choices words = new Choices();
        words.Add("Hi");
        words.Add("No");
        words.Add("Yes");

        Grammar wordsList = new Grammar(new GrammarBuilder(words));

        wordsList.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(rec_SpeechRecognized);
        sr.LoadGrammar(wordsList);

        sr.SetInputToDefaultAudioDevice();
        sr.RecognizeAsync();
    }


    void rec_SpeechRecognized(object sender, RecognitionEventArgs e)
    {
        MessageBox.Show(e.Result.Text);
    }

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

这似乎很有效。唯一的问题是,一旦它检测到单词&#34; hi&#34;就是这样,它就不会再出现问题了。 有没有办法让这个总是听?所以我可以说&#34;嗨&#34;,然后&#34;不&#34;,然后&#34;是&#34;。

我们希望在此基础上创建一个命令列表

感谢您的任何建议

2 个答案:

答案 0 :(得分:0)

来自tweellt的结论。

sr.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(rec_test);

然后再次调用

 private void rec_test(object sender, RecognizeCompletedEventArgs e)
    {
        sr.RecognizeAsync();
    }

答案 1 :(得分:0)

你可以改变

sr.RecognizeAsync();

BeginSpeach()

sr.RecognizeAsync(RecognizeMode.Multiple);

这将检测你说的一切......

Official Documentation

RecognizeAsync() - 执行单个异步语音识别操作。

RecognizeAsync(RecognizeMode) - 执行一项或多项异步语音识别操作。