这是我目前的代码: (之后我会解释我的问题)
Choices sList;
Grammar gr;
SpeechRecognitionEngine sRecognize = new SpeechRecognitionEngine();
private void BtnStartLuister_Click(object sender, EventArgs e)
{
sList = new Choices();
sList.Add(new string[] { "hallo", "ik", "ben", "Ken", "zoek", "exit", "leeg", "stop", "add" });
gr = new Grammar(new GrammarBuilder(sList));
BtnStartLuister.Enabled = false;
BtnStopLuister.Enabled = true;
try
{
//sRecognize.MaxAlternates = 0;
sRecognize.RequestRecognizerUpdate();
sRecognize.LoadGrammar(gr);
sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;
sRecognize.SetInputToDefaultAudioDevice();
sRecognize.RecognizeAsync(RecognizeMode.Multiple);
sRecognize.Recognize();
}
catch
{
return;
}
}
private void BtnStopLuister_Click(object sender, EventArgs e)
{
sRecognize.RecognizeAsyncStop();
BtnStartLuister.Enabled = true;
BtnStopLuister.Enabled = false;
}
private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
switch (e.Result.Text)
{
case "exit": { Application.Exit(); break; }
case "stop": { BtnStopLuister_Click(sender, e); break; }
case "zoek": { btnZoek_Click(sender, e); break; }
case "add": { VoegToeBtn_Click(sender, e); break; }
case "leeg": { tbWoord.Clear(); break; }
default: { tbWoord.Text = tbWoord.Text + e.Result.Text.ToString() + " "; break; }
}
}
因此,如果我点击' BtnStartLuister',它将开始收听sList中的单词。效果很好,并将它们输出到tbWoord(我的表单中的文本框),如果是默认情况。 现在实际问题是:如果我按下按钮' BtnStopLuister',它就会停止收听等等。如果我按下“BtnStartLuister”#39;再次,然后说"肯"例如,它输出" Ken"的两次即可。我试过调试,似乎有一些替代品,e.Result携带。对我很好,但我不希望他们输出到我的文本框。
有谁知道如何解决这个问题? 任何帮助将不胜感激:)
(顺便说一下,我对C#(和一般编程)很陌生,对不起,如果有一些变量名称部分用荷兰语表示)
答案 0 :(得分:2)
CodeCaster在评论中说了什么:
sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;
每次BtnStartLuister_Click方法运行时,您都会订阅该事件。取消订阅停止按钮事件处理程序。"