2014-04-12 83 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

您可以退訂,並通過設置這個空處置該對象,當你需要它,重建和重新訂閱。或者你只能退訂,它不會引發任何事件。