SpeechRecognizer Class and Exception 0x80045509

by Kevin June 26, 2013 22:40

In my game, Stroop Effect, I used the SpeechRecognizer class instead of the SpeechRecognizerUI class, because I needed a custom UI without the popup prompt. I was getting an exception that I didn't recognize while using the emulator, but the game worked fine on my Lumia 920, so I submitted it to beat the DVLUP Color Confusion challenge deadline. My game failed certification, because the tester had never used speech on the test device and he received an exception code of 0x80045509.

Speech Recognition Setting Speech Privacy Policy Prompt

While searching for the error code, I found that it was a speech privacy policy error, because the non-UI SpeechRecognizer class does not prompt the user if speech recognition is not enabled in the phone's settings. I decided to save a boolean setting to track whether speech is enabled and prompt the user using the SpeechRecognizerUI class in the main page when they first start a game. Once they reply yes to the speech prompt, I set the boolean setting to true and navigate the user to the game page. Be sure to include the 0x80045509 exception check whenever you use the SpeechRecognizer class, because the user may navigate back to the page after disabling speech recognition in the phone's settings.

Here is an example of the exception handling.

catch (Exception ex)
{
    const int privacyPolicyHResult = unchecked((int)0x80045509);
    const int networkNotAvailable = unchecked((int)0x80045504);

    if (ex.HResult == privacyPolicyHResult)
    {
        // User has not accepted the speech privacy policy
        this.PromptForSpeechRecognitionPermission();
    }
    else if (ex.HResult == networkNotAvailable)
    {
        this.Dispatcher.BeginInvoke(delegate { MessageBox.Show("Speech " +
            "recognition requires an active data connection. Please check " +
            "the data connection on your phone and try again.", "Error",
            MessageBoxButton.OK); });
    }
    else
    {
        this.Dispatcher.BeginInvoke(delegate { MessageBox.Show(ex.Message); });
    }
}

In Stroop Effect, I prompt the user on the game page only if the error occurs.

private async void PromptForSpeechRecognitionPermission()
{
    SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
    SpeechRecognizerUI speechRecognizerUI = new SpeechRecognizerUI();

    // Prompt the user using speech synthesis
    await speechSynthesizer.SpeakTextAsync("Please tap accept in the " +
        "following dialog box to enable speech recognition on your " +
        "phone, then say something to make sure the game can hear you.");

    // Required to get user's speech consent
    speechRecognizerUI.Settings.ExampleText = "Ex. \"Red\"";
    speechRecognizerUI.Settings.ListenText = "Listening...";
    speechRecognizerUI.Settings.ReadoutEnabled = true;
    speechRecognizerUI.Settings.ShowConfirmation = true;
    var result = await speechRecognizerUI.RecognizeWithUIAsync();
}

Tags: , , ,

Windows Phone

About the author

me

I work on .NET development by day; Windows PC & Windows Phone development by night.

Month List