2012-09-13 65 views
2

我需要編寫一個使用語音識別引擎的應用程序。如何通過C#語音在多個文本框中輸入不同的值

如何通過c#語音在多個文本框中輸入不同的值?

我可以在單個文本框中輸入值,但不能在第二個文本框中輸入值。我有以下代碼在單個文本框中輸入值。

private SpeechRecognitionEngine rec; 

private void voice()  
{ 
    rec = new SpeechRecognitionEngine(); 
    rec.SetInputToDefaultAudioDevice(); 
    Choices choice = new Choices("apple","Orange","Onion"); 
    GrammarBuilder gr = new GrammarBuilder(choice); 
    Grammar grammar = new Grammar(gr); 
    rec.LoadGrammar(grammar); 
    rec.SpeechRecognized += 
     new EventHandler<SpeechRecognizedEventArgs>(rec_SpeechRecogonized); 
    rec.RecognizeAsync(RecognizeMode.Multiple); 
} 

void rec_SpeechRecogonized(object sender, SpeechRecognizedEventArgs e) 
{ 
    foreach (RecognizedWordUnit word in e.Result.Words) 
    { 
     textBox1.Text = word.Text; 
    } 
} 
+0

爲什麼不使用某些識別的文字,如「命令」,以「專注」特定的文本框,然後任何其他認可的文本以下是放置在該文本框中... –

回答

0

我會做這樣的事情(很簡單的例子):

private SpeechRecognitionEngine rec; 

private void voice()  
{ 
    rec = new SpeechRecognitionEngine(); 
    rec.SetInputToDefaultAudioDevice(); 
    Choices choice = new Choices("apple","Orange","Onion", "next"); 
    GrammarBuilder gr = new GrammarBuilder(choice); 
    Grammar grammar = new Grammar(gr); 
    rec.LoadGrammar(grammar); 
    rec.SpeechRecognized += 
     new EventHandler<SpeechRecognizedEventArgs>(rec_SpeechRecogonized); 
    rec.RecognizeAsync(RecognizeMode.Multiple); 
} 

private TextBox currentInput; 
void rec_SpeechRecogonized(object sender, SpeechRecognizedEventArgs e) 
{ 
    if (currentInput == null) currentInput = textBox1; 

    foreach (RecognizedWordUnit word in e.Result.Words) 
    { 
     if (word.Text = "next") { currentInput = textBox2; } 
     else { currentInput.Text = word.Text; } 
    } 
} 
+0

感謝您的回答 –

+0

接受答案並提高您的聲譽得分;-) –

相關問題