gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:26:35 25 4
gpt4 key购买 nike

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

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

关于c# - C#中如何通过语音在多个文本框中输入不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12400227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com