コード例 #1
0
        private async Task changeStateAsync(ListeningState state)
        {
            if (listeningState != state)
            {
                System.Diagnostics.Debug.WriteLine($"Changing CommandInterpreter listening state to {state}");

                if (state == ListeningState.Hypothesis)
                {
                    preHypothesisListeningState = listeningState;
                }

                listeningState = state;

                await listener.StopListeningAsync();

                switch (state)
                {
                case ListeningState.Move:
                    SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.MoveCommands, true);
                    SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.PieceConfirmation, false);
                    SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.YesNoCommands, false);
                    SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.CancelCommand, false);
                    break;

                case ListeningState.PieceConfirmation:
                    SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.PieceConfirmation, true);
                    SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.MoveCommands, false);
                    SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.YesNoCommands, false);
                    SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.CancelCommand, true);
                    break;

                case ListeningState.Hypothesis:
                    SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.YesNoCommands, true);
                    SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.MoveCommands, false);
                    SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.PieceConfirmation, false);
                    SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.CancelCommand, true);
                    break;

                default:
                    throw new Exception("Tried to change CommandInterpreter state to an unknown listening state");
                }

                await listener.StartListeningAsync();
            }
        }