Windows Phone 8.1 Text-To-Speech - What’s changed for #WPDev?


If you are a WPDev, you might want to integrate the Text-To-Speech engine of WP8.x SDK in your application/game. Though it is not a new feature, but the API has been changed in Windows Phone 8.1 SDK which you might want to know before integrating.

 

In this blog post, I am going to share the API changes in Windows Phone 8.1 and do the comparison with WP8.0 APIs. Continue reading to know more about this.

 

If you are new into TTS engine in Windows Phone app development, you might want to read my other post “How to enable Text-to-Speech (TTS) in your #WP8 app” that describes about the implementation of the same in Windows Phone 8.0. You might be interested on the below mentioned blog posts too which describes about the API changes in Windows Phone 8.1 for WP developers:

 

Text-to-Speech in Windows Phone 8.0

In Windows Phone 8.0, it was quite easy to call the API to get the voice output from the input string. If you remember the code, we had to create the instance of the SpeechSynthesizer class in an async method and call the asynchronous method named SpeakTextAsync(string text) passing the input text string to it by specifying await keyword as shown in the below code snippet:

 

private async Task Speak(string text)
{
    using (var speech = new SpeechSynthesizer())
    {
         await speech.SpeakTextAsync(text);
    }
}

 

In Windows Phone 8.0, you had to enable ID_CAP_SPEECH_RECOGNITION capability in order to use Text-To-Speech engine:

 

How to enable the ID_CAP_SPEECH_RECOGNITION capability in your WP8 app

 

It was so simple in Windows Phone 8.0 SDK to integrate in our project. Just 2 lines of code and you are done. But, this has been changed a little bit in Windows Phone 8.1 and now you have to write few additional lines of code to implement it. Let’s discuss.

 

Text-to-Speech in Windows Phone 8.1

In Windows Phone 8.1 too, you have to write the code in an async method/event and have to create the instance of the SpeechSynthesizer class. Up to this, all codes are fine. Now comes the changes. Instead of calling the SpeakTextAsync(…) method now, we have another asynchronous API of the instance of the SpeechSynthesizer class named “SynthesizeTextToStreamAsync”. When you call this await able API passing the text string as input, it returns you the  voice stream of the input string. Now you have to set this voice stream object as the source of the MediaElement control to play the same. Here is the sample code for your reference:

 

public static async Task Speak(string text)
{
    using (var speech = new SpeechSynthesizer())
    {
        var voiceStream = await speech.SynthesizeTextToStreamAsync(text);
 
        // Either create an instance of the MediaElement object or place the control in view
        // If you are using it in multiple places over the application, 
        // create the instance in App.xaml.cs and access it globally.
        mediaElement.SetSource(voiceStream, voiceStream.ContentType);
        mediaElement.Play();
    }
}

 

Here the MediaElement control can be an private instance of the class or method. It can also be a control placed inside your XAML view. If you are using Text-to-Speech from different location of the application/game, it is better to create an instance in your App.xaml.cs file and access it across the application.

 

In Windows Phone 8.1, you must have to enable the Microphone capability in order to use Text-To-Speech engine:

How to enable the Microphone capability in your WP8.1 app

 

End Note

I hope that the explanation was simple and easy to understand. Now you will be able to easily integrate Text-to-Speech functionality in your application. I am going to share few more posts on the same topic to help you understand the new APIs of Windows Phone 8.1. Till that time, happy reading my other posts on Windows Phone 8.1 series.

 

Also, don’t forget to drop a line below mentioning your queries and/or feedback if any. I am available on Twitter, Facebook and Google+. Do connect with me to get my regular updates in your feed. Subscribe to my blog’s RSS Feed and Email Newsletter to get the immediate article update in your inbox.

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

2 comments

  1. Sir thanx for sharing idea to make this type of app.. Sir need help for making flashlight App

    ReplyDelete
  2. some new details surrounding the case and thought I would update you
    on what has happened as a result of this sad situation.
    The problem can be further expanded by collecting data from entire.
    Every cent earned by the business was used to pay for a nine-story building,
    where the first official National Book Store was located.


    Here is my weblog: portsmouth high school boosters ()

    ReplyDelete


 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top