Using Speech with Windows Phone 8

DSCF0579_80_81.jpg

I said yesterday that I’d submitted my latest version of Voice Music to Microsoft for certification.

It failed (sad face).

The reason it failed is interesting if you write speech applications for Windows Phone 8. It has to do with what happens if you interrupt the speech playback. One of the principles of the Windows Phone UI is that at any point when you are using a program you should be able to press the Windows Key on the phone and go off and do something else. Then, when you have finished doing the something else, you press the back key and return to the previous program. If your program doesn’t work like this it will fail certification, since this is a required behaviour.

It turns out that my version of Voice Music had a problem if the user pressed the Windows Key while it was announcing a message. They could go off and do something else, but when they pressed Back they did not return to Voice Music. This was a fairly nasty bug to spot, in that it only occurs when the program is running “in the wild”, i.e. when it is in the phone and not attached to the Visual Studio debugger. It also made finding the bug tricky, in that as soon as I tried to look for it the program started working again. Which was not very nice.

Eventually I used a technique that has worked before. I asked for help. And it turns out that the answer is quite simple. When a speaking program is interrupted by the user pressing the Windows Button (or generally moving away from the application) it will throw an exception when the program is next restarted by someone returning to it. The exception is thrown by the SpeakTextAsync method. If your program doesn’t catch this exception it will fail to restart. The exception has a particular type that you can check for if you wish:

try
{
    SpeechSynthesizer synth = new SpeechSynthesizer();
     await synth.SpeakTextAsync("Hello cheeky.");
 }
catch (Exception ex)
{
     if (((uint)ex.HResult == 0x80045508)) 
     {
        // System Call Interrupted thrown by Speech 
     }
 }

My program just ignores the exception, which seems to have fixed the problem. You can do more clever things if you wish. I’ve resubmitted it for certification. Fingers crossed….