【发布时间】:2015-03-30 07:29:15
【问题描述】:
我正在尝试在 C# 桌面应用程序中使用语音识别,如果用户说“按 3”或“按 4”,那么该数字应该写在图表上(比如标签)。我能够识别出用户说出的单词“press”,但除此之外。请帮忙。以下是我的示例代码:
string txtSpoken = "";
string[] words = new string[10];
public Form1()
{
InitializeComponent();
SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
_recognizer.LoadGrammar(new Grammar(new GrammarBuilder("press")) { Name = "pressGrammar" }); // load a grammar
_recognizer.SpeechRecognized += _recognizer_SpeechRecognized;
_recognizer.SetInputToDefaultAudioDevice(); // set the input of the speech recognizer to the default audio device
_recognizer.RecognizeAsync(RecognizeMode.Multiple); // recognize speech asynchronous
// _recognizer.Recognize();
}
void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string txt = e.Result.Text;
this.Invoke(new MethodInvoker(() =>
{
listBox1.Items.Add("I heard you say: "
+ txt);
})); // WinForm specific
if (e.Result.Text == "press") // e.Result.Text contains the recognized text
{
textBox1.Text = "3";
label1.Text += " 3 ";
// MessageBox.Show("The test was successful!");
}
txtSpoken += e.Result.Text;
MessageBox.Show(txtSpoken);
if (txt.IndexOf("press") >= 0)
{
words = txt.Split(' ');
}
}
【问题讨论】:
-
txt的内容是什么?
标签: c#