在我的UWP应用程序中,语音未被默认听写语法识别。

时间:2016-05-03 05:22:30

标签: win-universal-app windows-10 speech-recognition windows-10-universal

我的UWP应用程序中没有使用默认听写语法识别语音。但是,当我使用程序列表约束时,它是完全可以识别的。下面是我的代码的语音识别部分供参考。如果我不评论第5行,这很好。我在下面做错了什么:

            speechRecognizer = new SpeechRecognizer();
            bool PermissionGained = await CheckMicrophonePermission();
            if (PermissionGained)
            {
               //speechRecognizer.Constraints.Add(new SpeechRecognitionListConstraint(Grammar.GrammarCommands.GrammarConstraintList));
               await speechRecognizer.CompileConstraintsAsync();

                //recognize speech input at any point of time 
                speechRecognizer.ContinuousRecognitionSession.ResultGenerated +=
                    async (s, e1) =>
                    {
                        if ((e1.Result != null)) 
                        {
                            await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                async () =>
                                {
                                    await ParsespeechCommand(e1.Result);

                                });
                             speechRecognizer.ContinuousRecognitionSession.Resume();
                        }
                    };
                await speechRecognizer.ContinuousRecognitionSession.StartAsync(SpeechContinuousRecognitionMode.PauseOnRecognition);
            }

1 个答案:

答案 0 :(得分:0)

如上所述,我做了一个演示,并从代码中做了一些更改。 这是我的代码:

private async void myBtn_Click(object sender, RoutedEventArgs e)
{
        //here I remove the checkPermission process
        var speechRecognizer = new SpeechRecognizer();
        if (true)
        {
            //speechRecognizer.Constraints.Add(new SpeechRecognitionListConstraint(new List<string> { "winffee", "Elvis", "noob" }));
            await speechRecognizer.CompileConstraintsAsync();

            //recognize speech input at any point of time 
            speechRecognizer.ContinuousRecognitionSession.ResultGenerated +=
                async (s, e1) =>
                {
                    if ((e1.Result != null))
                    {
                        await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                            () =>//here I remove the async
                            {
                                var result = e1.Result;//here I remove the method to focus on the e1.Result.
                            });
                        speechRecognizer.ContinuousRecognitionSession.Resume();
                    }
                };
            await speechRecognizer.ContinuousRecognitionSession.StartAsync(SpeechContinuousRecognitionMode.PauseOnRecognition);
        }
}

整个功能由按钮点击事件触发。我对每个变化位置(共3个位置)发表评论。