2

我需要编写一个使用语音识别引擎的应用程序。

如何在 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;
    }
}
4

1 回答 1

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; }
    }
}
于 2012-09-13T06:38:01.040 回答