系统语音切换按钮逻辑

时间:2019-01-29 03:44:11

标签: c# .net system.speech.recognition

我有一个c#.net按钮,该按钮应该用于打开和关闭Windows system.speech识别。基本上打开和关闭监听。它是连续的,因此一旦激活,它将在每次听到“ Hey Apple”时继续监听并触发识别事件,直到再次单击该按钮将其停止。但是,它似乎只能工作一次或两次,然后它将停止识别“嘿苹果”,有时甚至即使在我再次单击应该放置识别会话的按钮之后也可以识别。是背后的按钮逻辑搞砸了还是我错过了语音识别的某些部分?

internal class Button1 : Button
{
    private bool isCheck = true;

    protected override void OnClick()
    {
        SpeechSynthesizer ss = new SpeechSynthesizer();
        PromptBuilder pb = new PromptBuilder();
        SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
        Choices keyWords = new Choices(new string[] { "Hey Apple", "hey apple" });
        Grammar gr = new Grammar(new GrammarBuilder(keyWords));

        if (isCheck == true)
        {

            sre.RequestRecognizerUpdate();
            sre.LoadGrammar(gr);
            sre.SpeechRecognized += sre_SpeechRecognized;
            sre.SetInputToDefaultAudioDevice();
            sre.RecognizeAsync(RecognizeMode.Multiple);
            Debug.WriteLine("I'M LISTENING");
            Caption = "Listening2u";
            isCheck = false;
        }
        else
        {
            Caption = "Not Listening2u";
            sre.RecognizeAsyncCancel();
            sre.Dispose();
            Debug.WriteLine("NOT LISTENING");
            isCheck = true;
        }

    }
    async void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        SoundPlayer player = new SoundPlayer(Properties.Resources.beep1);
        player.Play();
        Debug.WriteLine("recognized = " + e.Result.Text.ToString());
    }


}

0 个答案:

没有答案