Dictation Recognizer stops working when the app loses focus

When a Unity application that has an active Dictation Recognizer loses focus, the recognition is cancelled with the “Dictation completed unsuccessfully: Canceled.” error, even with the app having “Run in background” checked. Although if the app does NOT have focus to begin with when the recognizer is started, the recognizer works just fine in the background which leaves me a little bit confused.

Does anyone have an idea how I could prevent the recognizer from stopping when the app loses focus? I thought about simply just restarting the recognition immediately when focus is lost, but if the focus is lost in the middle of a sentence, the first part of it will be lost obviously. Or I could just unfocus the app (which seems pretty problematic to do) first and then start the recognizer but this could cause all kinds of problems.

The reproduce the issue use the code from here:

Attach it to a GO, run the game, and then while the recognition is active click outside of the editor (unfocus the editor).

I don’t see any additional settings to play with to try and make it work in background; this might just be a good old fashioned Unity bug for the bug reporter.

Do you get the same effect in editor as well as in a standalone build?

Do all your other scripts continue to execute normally while in background?

I thought it was a bug as well, so I have submitted a report already. Yes, everything runs normally in the background except for the Dictation Recognizer, both in the editor and in standalone.

I have also tried using System.Speech.Recognition in a Visual Studio WPF application and that one has no such problems, but I don’t know how much relation is there between UnityEngine.Windows.Speech and System.Speech.Recognition.

Given that it only works on Windows, I would guess that the UnityEngine one is just a more Unity-friendly wrapper around the System.Speech.Recognition code. Can you try using the System one directly?

It seems like there is no System.Speech.Recognition in Unity, but since that one does not handle free dictation well, I wouldn’t want to use it anyway.

@Weightless Did you ever find a resolution to this problem by any chance?

2 Likes

I still am encountering this problem… Anyone a solution?

I have the same problem !!!

I am writing an application to take voice commands and then send keystrokes based on those commands to other applications. So, my use case requires that the speech recognition work while the window is unfocused. I am using the KeywordRecognizer, rather than the DictationRecognizer, so my situation is a little different, but I was encountering a similar error. Since I’m not worried about losing window focus while speech is happening, I was able to solve it by restarting the PhraseRecognitionSystem when the window loses focus. I included the following method in my monobehaviour responsible for handling the speech recognition:

private void OnApplicationFocus(bool focus)
    {
        if (!focus)
        {
            keywordRecognizer.Stop();
            PhraseRecognitionSystem.Shutdown();
            keywordRecognizer.Start();
        }
    }

The if statement ensures that the code only runs when the window is losing focus, not when it’s gaining focus. The KeywordRecognizer is initially started in the monobehaviour’s Start() method.
I was only able to get this to work by using PhraseRecognitionSystem.Shutdown(), PhraseRecognitionSystem.Restart() would not work. This feels like a very shaky workaround to the problem, and it’s entirely possible that it’s luck dependent on the order that the OS handles the windows or something, but I hope this is helpful to someone anyway.

I tried your method but it does not seem to be working for me. I need it to work when either minimized or not in focus. Are there any alternatives?

i found the solution to this thing already, note this bug only happens with a build, is not affected while on the editor, atleast that was my case, anyways just copy and paste this code below wherever the voice recognition script is, and you’re good to go,

private void OnApplicationFocus(bool isFocused)
{

Debug.Log($“Is App focused? : {isFocused}”);

if (!isFocused)
{
PhraseRecognitionSystem.Shutdown();
Debug.LogError(“LOST FOCUS OF THE GAME ATTEMPING TO FIX…, (NOT AN ERROR)”);
}
else
{
PhraseRecognitionSystem.Restart();
Debug.LogError(“RECOGNIZER RESET, TRY NOW”);

}

}