5. Voice Commands - sorianog/RollABallMR GitHub Wiki

The final addition to the “Roll-a-ball” project was the implementation of voice commands. The chosen voice commands to include were: “Pause Game”, “Resume Game”, “Reset Game”, and “Exit Game”. “Chapter 4 - Voice” of the “Holograms 101” tutorial was followed to implement a speech manager for “Roll a Ball - Holographic”. The “UnityEngine.Windows.Speech” library was used to create a “KeywordRecognizer” to observe the keywords specified for the four chosen voice commands. The following code in the SpeechManager script determines what should happen when the voice commands as vocalized.

    void Start()
    {
        keywords.Add("Pause Game", () =>
        {
            AudioListener.volume = 0;
            Time.timeScale = 0;
        });

        keywords.Add("Resume Game", () =>
        {
            AudioListener.volume = 1;
            Time.timeScale = 1;
        });

        keywords.Add("Reset Game", () =>
        {
            // Call the OnReset method on every descendant object.
            this.BroadcastMessage("OnReset");
        });

        keywords.Add("Exit Game", () =>
        {
            Application.Quit();
        });

        // Tell the KeywordRecognizer about our keywords.
        keywordRecognizer = new KeywordRecognizer(keywords.Keys.ToArray());

        // Register a callback for the KeywordRecognizer and start recognizing!
        keywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
        keywordRecognizer.Start();
    }

Pausing/Resuming a game will stop/resume the audio and game play. When resetting a game, the “OnReset” method will be called on the game to set the Score to 0, place any picked up rotating cubes back into the game, and clearing the victory text. Voice commands were picked up well on the HoloLens and there was no noticeable latency between the spoken command the command being executed in the game.