Search Unity

Dictation Recognizer stops working when the app loses focus

Discussion in 'Scripting' started by Weightless, May 14, 2020.

  1. Weightless

    Weightless

    Joined:
    Sep 7, 2017
    Posts:
    15
    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:
    https://docs.unity3d.com/2017.3/Doc...rence/Windows.Speech.DictationRecognizer.html
    Attach it to a GO, run the game, and then while the recognition is active click outside of the editor (unfocus the editor).
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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?
     
  3. Weightless

    Weightless

    Joined:
    Sep 7, 2017
    Posts:
    15
    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.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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?
     
  5. Weightless

    Weightless

    Joined:
    Sep 7, 2017
    Posts:
    15
    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.
     
  6. nostalgicbear

    nostalgicbear

    Joined:
    Mar 21, 2013
    Posts:
    99
    @Weightless Did you ever find a resolution to this problem by any chance?
     
  7. unity_uCAh1YGgjxXPOw

    unity_uCAh1YGgjxXPOw

    Joined:
    Feb 10, 2021
    Posts:
    6
    I still am encountering this problem... Anyone a solution?
     
  8. drin1drin

    drin1drin

    Joined:
    Jun 8, 2020
    Posts:
    10
    I have the same problem !!!
     
  9. Cosmonaut800

    Cosmonaut800

    Joined:
    Oct 3, 2018
    Posts:
    1
    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:

    Code (CSharp):
    1. private void OnApplicationFocus(bool focus)
    2.     {
    3.         if (!focus)
    4.         {
    5.             keywordRecognizer.Stop();
    6.             PhraseRecognitionSystem.Shutdown();
    7.             keywordRecognizer.Start();
    8.         }
    9.     }
    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.
     
  10. SenshiBuredo

    SenshiBuredo

    Joined:
    Feb 8, 2019
    Posts:
    2
    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?
     
  11. williamhiciano26

    williamhiciano26

    Joined:
    Feb 24, 2021
    Posts:
    2
    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");

    }

    }