问题
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