Speech is not being recognized with default dictation grammar in my UWP application.

删除回忆录丶 提交于 2019-12-12 03:09:01

问题


Speech is not being recognized with default dictation grammar in my UWP application. However, it is perfectly recognized when I use programmatic list constraint. Below is the speech recognition part of my code for reference. If I do not comment the 5th line, this works fine. Am I doing something wrong below:

            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:


As discussed I made a demo and made some changes from the codes. Here are my codes:

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);
        }
}

The whole function is triggered by a button click event. And I made comment on every change position(totally 3 places).



来源:https://stackoverflow.com/questions/36996184/speech-is-not-being-recognized-with-default-dictation-grammar-in-my-uwp-applicat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!