winforms SpeechRecognitionEngine

我的梦境 提交于 2019-12-11 06:51:07

问题


we have started to play about with the speechRecognitionEngine, and built a very basic app based on one that we found on stack overflow. Code below:-

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();
    }
}

This seems to work great. The only issue is that once it has detected the word "hi", thats is, it won't defect any more. Is there a way to get this to always listen? so i can say "hi", then "no", then "yes".

We hope to build on this to create a list of commands

Thank you for any advice


回答1:


Came to the conclusion from tweellt.

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

and then calling this again

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



回答2:


You could just change

sr.RecognizeAsync();

in BeginSpeach() to

sr.RecognizeAsync(RecognizeMode.Multiple);

This will detect everything you say...

Official Documentation

RecognizeAsync() - Performs a single, asynchronous speech recognition operation.

RecognizeAsync(RecognizeMode) - Performs one or more asynchronous speech recognition operations.



来源:https://stackoverflow.com/questions/22332157/winforms-speechrecognitionengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!