Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

DictationRecognizer.Status is "Running" but isn't picking up anything

Discussion in 'Scripting' started by auroraland, Aug 12, 2019.

  1. auroraland

    auroraland

    Joined:
    Dec 7, 2017
    Posts:
    5
    Hey guys, I'm using DictationRecognizer for speech detection and am having troubles.

    I got a basic empty scene with one game object, with a script that hooks up to DictationRecognizer and its emitted events. This script uses a while(true) coroutine that checks DictationRecognizer's status.

    I initialize DR timeouts to 10 seconds and hook up callbacks to Hypothesis, Result, Complete and Error events. On Complete (includes timeouts), I Stop() and Start() DR again, so the DR is always live and listening.

    It does its job well for a good while, but will have these intermittent periods where it doesn't pick up at all, even though the monitor coroutine says its status is Running.

    I've gone about this several ways, from completely disposing of the object, and re-initializing it and restarting it, to just simply Starting and Stopping it, and I'm at a loss.

    Has anyone observed the same? If so, how did you deal with it?

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. #if (UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_WINRT || UNITY_WINRT_8_0 || UNITY_WINRT_8_1 || UNITY_WINRT_10_0)
    6. using UnityEngine.Windows.Speech;
    7. #endif
    8.  
    9. public sealed class TestVCService : MonoBehaviour
    10. {
    11. #if (UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_WINRT || UNITY_WINRT_8_0 || UNITY_WINRT_8_1 || UNITY_WINRT_10_0)
    12.  
    13.    ConfidenceLevel _minimumConfidence = ConfidenceLevel.Low;
    14.    DictationRecognizer _dictationRecognizer;
    15.    Coroutine _serviceLifeCheckerCoroutine;
    16.  
    17.    void Start()
    18.    {
    19.       InitializeRecognizer();
    20.       StartRecording();
    21.    }
    22.  
    23.    void OnDestroy()
    24.    {
    25.       if (_dictationRecognizer != null)
    26.       {
    27.          _dictationRecognizer.Dispose();
    28.       }
    29.    }
    30.  
    31.    IEnumerator CheckDictationRecognizerStatusRoutine()
    32.    {
    33.       while (true)
    34.       {
    35.          print(_dictationRecognizer.Status);
    36.          yield return null;
    37.       }
    38.    }
    39.  
    40.    void InitializeRecognizer()
    41.    {
    42.       _dictationRecognizer = new DictationRecognizer(_minimumConfidence, DictationTopicConstraint.Dictation)
    43.       {
    44.          InitialSilenceTimeoutSeconds = 10, AutoSilenceTimeoutSeconds = 10
    45.       };
    46.       _dictationRecognizer.DictationResult += OnDictationResult;
    47.       _dictationRecognizer.DictationHypothesis += OnDictationHypothesis;
    48.       _dictationRecognizer.DictationComplete += OnDictationComplete;
    49.       _dictationRecognizer.DictationError += OnDictationError;
    50.    }
    51.  
    52.    void StartRecording()
    53.    {
    54.       try
    55.       {
    56.          _dictationRecognizer.Start();
    57.          _serviceLifeCheckerCoroutine = StartCoroutine(CheckDictationRecognizerStatusRoutine());
    58.       }
    59.       catch (UnityException ue)
    60.       {
    61.          print(ue.Message);
    62.       }
    63.    }
    64.  
    65.    void StopRecording()
    66.    {
    67.       _dictationRecognizer.Stop();
    68.       StopCoroutine(_serviceLifeCheckerCoroutine);
    69.    }
    70.  
    71.    void OnDictationResult(string text, ConfidenceLevel confidence)
    72.    {
    73.       print("Dictation result: " + text);
    74.    }
    75.  
    76.    void OnDictationHypothesis(string text)
    77.    {
    78.       print("Dictation result: " + text);
    79.    }
    80.  
    81.    void OnDictationComplete(DictationCompletionCause cause)
    82.    {
    83.       print(cause);
    84.       StopRecording();
    85.       StartRecording();
    86.    }
    87.  
    88.    void OnDictationError(string error, int hresult)
    89.    {
    90.       print("Dictation error - " + error + "; HResult = " + hresult);
    91.    }
    92. #else
    93.    void Start()
    94.     {
    95.         print("This service is only supported on Windows.");
    96.     }
    97. #endif
    98. }
    99.