2014-04-12 84 views
0

我正在尝试制作语音识别器应用程序。使用C#。我创建了基本的应用程序,它工作正常。如何设置语音识别器启动和停止

现在我的问题是。

我该如何使用按钮控件来启动和停止语音识别器。

我使用SpeechRecognitionEngine

这里是我的代码。

private bool Status = false; 

    SpeechRecognitionEngine sre = new SpeechRecognitionEngine(); 
    Choices dic = new Choices(new String[] { 
       "word1", 
       "word2", 
       }); 



    public Form1() 
    { 
     InitializeComponent(); 

     Grammar gmr = new Grammar(new GrammarBuilder(dic)); 
     gmr.Name = "myGMR"; 
     // My Dic 

     sre.LoadGrammar(gmr); 
     sre.SpeechRecognized += 
     new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized); 
     sre.SetInputToDefaultAudioDevice(); 
     sre.RecognizeAsync(RecognizeMode.Multiple); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (Status) 
     { 
      button1.Text = "START"; 
      Status = false; 
      stslable.Text = "Stopped"; 
     } 
     else { 

      button1.Text = "STOP"; 
      Status = true; 
      stslable.Text = "Started"; 
     } 
    } 

    public void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs ev) 
    { 
     String theText = ev.Result.Text; 
     MessageBox.Show(theText); 
    } 

回答

1

您可以退订,并通过设置这个空处置该对象,当你需要它,重建和重新订阅。或者你只能退订,它不会引发任何事件。