问题
I am writing a speech recognition program using system.speech from MS. I have been going through the online tutorials and all the great info on StackOverflow however i seem to keep running into an issue where the recognizer seems to throw an error.
Below is the code I am using (minus the grammar creation).
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();
}
}
It recognizes the first phrase correctly but as soon as it completes the actions in the speechrecognized handler, it throws the exception "Cannot perform this operation while the recognizer is doing recognition.". I have tried with the recognition being all in a single method however it has the same results. This was my most recent attempt prior to posting this question. What am I doing wrong?
As to clarify...
The program launches into the systray and calls this class.OpenEars(). OpenEars then calls class.Listening() which has the RecognizeAsync. After speaking the first phrase and the recognizer hearing it correctly and following the handler, the second phrase when spoken ends up triggering the exception.
回答1:
The problem is that you are calling RecognizeAsync from the SpeechRecognized event handler. It is throwing the exception because the previous recognition has not yet completed. The event handler is blocking it from completing. Try starting a different task/thread to call RecognizeAsync.
来源:https://stackoverflow.com/questions/18774802/system-speech-recognition-error