我需要编写一个使用语音识别引擎的应用程序。
如何在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; }
}
}