Ever since the Windows Phone 8 SDK leaked, I have been digging through the documentation looking for interesting additions to the upcoming release. After spending a couple of evenings trying to get the Wallet to work in the emulator using sample code found in the docs to no avail, I turned my attention to the new SpeechSynthesizer and InstalledVoices.All classes.
As per my usual format, I start with the XAML. The first thing WP7 developers might notice is that the ApplicationTitle and pagenames are no longer hard coded by default but are resources shared across the app. This way you no longer have to set the app name on all your pages one at a time.
Next is the code-behind file for the MainPage.xaml. Its content is heavily borrowed for the WP8 SDK docs. I added some error checking and the ability to choose the gender and language of the voice used.
// Initialize the SpeechSynthesizer object.
// Query for a voice. Results rdered by Gender to ensure the order always goes Female then Male.
// gender: 0 = Female, 1 = Male. Corresponds to the index of the above results.
// Set the voice as identified by the query.
And finally is a couple of classes to hep fill the ComboBox of languages. Checking InstalledVoices.All shows me there are 30 items in it(15 languages x 2 voices each). I was unable to directly vies the list for reasons I don't know. All I know is that it is a COM object and the only way to get the required VoiceInformation objects needed is via a LINQ query. I was able to determine 12 languages, but need to look deeper to find out the other 3.
Next thing is to set the ID_CAP_SPEECH_RECOGNITION capability otherwise an exception is thrown.
Now if we run the app, we are shown the following. Choosing a language and clicking the button will cause the emulator to start talking to you.
Published at DZone with permission of Adam Benoit , DZone MVB. See the original article here.