2

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 ?

Antoine Thiry
  • 2,362
  • 4
  • 28
  • 42
  • Short question: Did you ever find an answer to your question? If yes, would you mind to provide information on how you solved this problem? But also what I am interested: How did you manage that Unity starts recognizing your words and is for example printing some messages on a canvas? I added the "System.Speech.dll" inside the assets folder, wrote the same code from your "InitSpeechEngine" function in my Start() method with the "recEngine.RecognizeAsync(RecognizeMode.Multiple);" at the end, but for me it doesn't seem to work. Am I missing something? I am using Unity 2019.3.12f1 Personal. – TheHeroOfTime May 30 '20 at 20:36
  • 1
    Hello, I ended up using paid cloud recognition services from IBM, I needed continuous detection and the Speech engine from microsoft is not that great for this.. Don't forget to hook the `SpeechRecognized` event from the SpeechRecognitionEngine :) – Antoine Thiry May 31 '20 at 10:47
  • Thanks for your answer. Your other solution sound interesting, but before I go further on such solution, I would like to try it with Speech engine from Microsoft. With "SpeechRecognized" you mean something like this (which I actually already have done): `recEngine.LoadGrammarAsync(grammar);` `recEngine.SetInputToDefaultAudioDevice();` `recEngine.SpeechRecognized += RecEngine_SpeechRecognized;` `recEngine.RecognizeAsync(RecognizeMode.Multiple);` This code is in the Start() method of my C# file. I assigned the C# file to the main camera, but every time when I speak, it wont work. Any ideas? – TheHeroOfTime May 31 '20 at 11:43
  • 1
    Your code seems fine, appart from telling you to check that you have actual input from your input device I can't really assist you further with that. Wrote that almost 2 years ago haha – Antoine Thiry May 31 '20 at 12:42
  • 1
    I just found another solution on how to solve this. But it is still good to see what other solutions are out there, so thank you very much. Haha, don't worry, I am grateful that you tried to help me and I think that's really cool, so thanks dude :D – TheHeroOfTime May 31 '20 at 14:04
  • If you want to recognize speech continuously, then why would you want to stop the speech recognition engine? – IneedHelp Sep 13 '20 at 07:06
  • @IneedHelp Because in the game context, the speech is not continuously monitored, it is being monitored during a certain game state, for that I need to stop the recognition when the player is not in the speech state. (i.e. in menu, settings, or doing something else). – Antoine Thiry Sep 14 '20 at 08:36
  • @AntoineThiry have you considered not stopping the speech recognition engine at all and just skip incrementing your counter when the user is not in the context where the counter should get increased? – IneedHelp Sep 14 '20 at 09:50
  • @IneedHelp That would be bad practice imo, as I might want to do some processing of the speech generated after and just not care about speech recognition engine that might throw some exceptions at some point. And as I said in my question, Unity crashed after I try to restart the game when the speech recognition is on :) Since this question is two years old, you might guess that I came up with another system to achieve what I wanted to do (IBM Cloud speech recognition is great). – Antoine Thiry Sep 14 '20 at 13:30

0 Answers0