繁体   English   中英

如何在C#中通过语音在多个文本框中输入不同的值

[英]how to enter different values in multiple textboxes through voice in 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;
    }
}

我将执行以下操作(非常简单的示例):

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; }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM