I'm using System.Speech
within Unity 2018.2 to perform some words check while the player is speaking.
Everything is fine, the word recognition is working but I can't stop the SpeechRecognitionEngine
.
Here's how I initialize the SpeechRecognitionEngine
:
void InitSpeechEngine()
{
_speechEngine = new SpeechRecognitionEngine();
Choices fillers = new Choices();
fillers.Add(FillerWords.ToArray());
GrammarBuilder gb = new GrammarBuilder();
gb.Append(fillers);
gb.Culture = new System.Globalization.CultureInfo("en-US");
_grammar = new Grammar(gb);
_speechEngine.LoadGrammarAsync(_grammar);
_speechEngine.SetInputToDefaultAudioDevice();
_speechEngine.SpeechRecognized += FillerRecognized;
}
How I start it :
public void StartSpeech()
{
_speechStarted = true;
_speechEngine.RecognizeAsync(RecognizeMode.Multiple);
}
And how I try to stop it :
public void StopSpeech()
{
_speechStarted = false;
_speechEngine.RecognizeAsyncCancel();
}
So this is not working the error message I have is :
InvalidOperationException: Cannot perform this operation while the recognizer is doing recognition.
What I want to do is monitoring the player speech for certain words and increment a counter when the player says the word. So I need continuous recognition and no push to talk, so the process should be totally invisible for the player. It's working as I want it to work for now, but I can't stop the SpeecehRecognitionEngine...
And I think that due to this error, my Unity editor crashes when I want to start the game again in the editor.
How should I stop the recognition ?