我写使用system.speech从MS语音识别程序。 我一直在经历的在线教程和StackOverflow上所有伟大的信息,但是我似乎继续运行到一个问题,即识别似乎抛出一个错误。
下面是我使用的代码(减去语法创建)。
Grammar grammarQuestionsSingle;
Grammar grammarQuestionsShort;
Grammar grammarQuestionsLong;
Grammar grammarStatement;
//Grammar grammarDeclarationShort;
//Grammar grammarDeclarationLong;
Grammar grammarCommandsSingle;
Grammar grammarCommandsShort;
Grammar grammarCommandsLong;
SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine();
CreateGrammar grammar = new CreateGrammar();
Think brain = new Think();
bool privacy, completed;
//bool timer;
public void OpenEars()
{
completed = true;
if (grammarQuestionsSingle == null || grammarQuestionsShort == null || grammarQuestionsLong == null || grammarStatement == null || grammarCommandsSingle == null || grammarCommandsShort == null || grammarCommandsLong == null)
{
grammarQuestionsSingle = grammar.createGrammarQuestionsSingle();
grammarQuestionsShort = grammar.createGrammarQuestionsShort();
grammarQuestionsLong = grammar.createGrammarQuestionsLong();
grammarStatement = grammar.createGrammarStatement();
grammarCommandsSingle = grammar.createGrammarCommandsSingle();
grammarCommandsShort = grammar.createGrammarCommandsShort();
grammarCommandsLong = grammar.createGrammarCommandsLong();
}
recognizer.RequestRecognizerUpdate();
if (!grammarQuestionsSingle.Loaded)
{
recognizer.LoadGrammar(grammarQuestionsSingle);
}
if (!grammarQuestionsShort.Loaded)
{
recognizer.LoadGrammar(grammarQuestionsShort);
}
if (!grammarQuestionsLong.Loaded)
{
recognizer.LoadGrammar(grammarQuestionsLong);
}
if (!grammarStatement.Loaded)
{
recognizer.LoadGrammar(grammarStatement);
}
if (!grammarCommandsSingle.Loaded)
{
recognizer.LoadGrammar(grammarCommandsSingle);
}
if (!grammarCommandsShort.Loaded)
{
recognizer.LoadGrammar(grammarCommandsShort);
}
if (!grammarCommandsLong.Loaded)
{
recognizer.LoadGrammar(grammarCommandsLong);
}
DictationGrammar dictationGrammar = new DictationGrammar("grammar:dictation");
dictationGrammar.Name = "DictationQuestion";
recognizer.LoadGrammar(dictationGrammar);
recognizer.RequestRecognizerUpdate();
recognizer.SetInputToDefaultAudioDevice();
Listening();
}
public void Listening()
{
while (!completed)
{
Thread.Sleep(333);
}
recognizer.SpeechRecognized += recognizer_SpeechRecognized;
recognizer.RecognizeAsync(RecognizeMode.Single);
}
private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
completed = false;
SemanticValue sem = e.Result.Semantics;
if (!privacy)
{
if (e.Result.Grammar.Name=="CommandsSingle" && sem["keyCommandsSingle"].Value.ToString() == "go to sleep")
{
privacy = true;
brain.useMouth("ear muffs are on");
completed = true;
Listening();
}
else
{
brain.Understanding(sender, e);
completed = true;
}
}
else
{
if (e.Result.Grammar.Name == "CommandsSingle" && sem["keyCommandsSingle"].Value.ToString() == "wake up")
{
privacy = false;
brain.useMouth("I am listening again");
completed = true;
Listening();
}
}
completed = true;
Listening();
}
}
它正确,但只要认识到第一阶段,因为它完成的speechrecognized处理器的操作,它会抛出异常“而识别是做肯定无法执行此操作。” 我曾尝试与识别正在然而,它具有相同的结果,一个单一的方法都。 这是我最近张贴了这个问题之前尝试。 我究竟做错了什么?
作为澄清...
该程序启动到系统托盘,并调用这个class.OpenEars()。 OpenEars然后调用它具有RecognizeAsync class.Listening()。 正确地说前半句和识别听到它并按照处理程序后,说话时的第二句话最终引发了异常。