2013-04-14 56 views
0

我創建了一個使用語音識別的C#項目,其中我有一個具有下一個和最後一個按鈕的表單我想要做的是當我說下一個按鈕將帶我到下一個文件,或者如果我回說它會去前一個文件。但是,在調試項目時,它只顯示我所說的內容,而不是這樣做。有誰知道我該如何修復它?使用語音識別的命令

這是我做的代碼:

private void Form1_Load(object sender, EventArgs e) 
    { 
     SpeechRecognizer recognizer = new SpeechRecognizer(); 
     Choices command = new Choices(); 
     command.Add(new string[] { "next", "last", "first" }); 

     GrammarBuilder gb = new GrammarBuilder(); 
     gb.Append(command); 

     Grammar g = new Grammar(gb); 
     recognizer.LoadGrammar(g); 

     recognizer.SpeechRecognized += 
       new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized); 
    } 


    void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
    { 
     MessageBox.Show("Speech recognized: " + e.Result.Text); 
    } 
} 
+0

更多的細節來幫助你...... – CodeGuy

+0

我說我做了 –

+1

假設我們所看到的主要大宗代碼的代碼,你沒有任何代碼來執行'next'或「前一個」行動。該代碼應該從'sre_SpeechRecognized'事件處理程序中調用。 – keyboardP

回答

2

幾年前我對這個問題的案例研究。如果你比較我的代碼和你的代碼,你可以找到一些東西。下面的代碼改變了燈泡的狀態。需要

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Speech.Recognition; 
using System.Threading; 


namespace SesTanima 
{ 
    public partial class Form1 : Form 
    { 
     private SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(); 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

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


     private void LoadGrammars() 
     { 
      Choices choices = new Choices(new string[] {"Lights on", "Exit", "Zoom out", "Zoom in", "Reset", "Lights off" }); 
      GrammarBuilder grammarBuilder = new GrammarBuilder(choices); 
      Grammar grammar = new Grammar(grammarBuilder); 
      recognizer.LoadGrammar(grammar); 
     } 

     private void StartRecognition() 
     { 
      recognizer.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected); 
      recognizer.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected); 
      recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized); 
      recognizer.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted); 


      Thread t1 = new Thread(delegate() 
      {    
       recognizer.SetInputToDefaultAudioDevice(); 
       recognizer.RecognizeAsync(RecognizeMode.Single); 
      }); 
      t1.Start(); 
     } 

     private void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs e) 
     { 
      textBox1.Text = "Recognizing voice command...";  
     } 

     private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
     { 
      if (e.Result.Text == "Lights on") 
      { 
       pictureBox1.Image = Properties.Resources.lightsOn; 
      } 
      else if (e.Result.Text == "Lights off") 
      { 
       pictureBox1.Image = Properties.Resources.lightsOff; 
      } 
      else if (e.Result.Text == "Exit") 
      { 
       recognizer.Dispose(); 
       Application.Exit(); 

      } 
      else if (e.Result.Text == "Zoom out") 
      { 
       pictureBox1.Size = new System.Drawing.Size(135, 107); 
      } 
      else if (e.Result.Text == "Zoom in") 
      { 
       pictureBox1.Size = new System.Drawing.Size(538, 426); 
      } 
      else if (e.Result.Text == "Reset") 
      { 
       pictureBox1.Size = new System.Drawing.Size(269, 213); 
      } 
      textBox1.Text = e.Result.Text; 
     } 

     private void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e) 
     { 
      textBox1.Text = "Failure."; 
     } 

     private void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e) 
     { 
      recognizer.RecognizeAsync(); 
     } 
    } 
} 
+0

非常感謝你Ozkan Ozlu –