How to enable Text-to-Speech (TTS) in your #WP8 app


It was a most demanding features by the Windows Phone community to include the Text-to-Speech engine in Windows Phone and the WPDevs who were missing it in Windows Phone 7 can now use this feature in their Windows Phone 8 applications.

 

The TTS engine is now available in WP8 devices which you can easily integrate in your app. This small post will describe how to use it.

 

Prerequisite

The first and foremost thing that you need to develop Windows Phone 8 applications are: 64-bit Windows 8 or Windows 8.1 operating system and Visual Studio 2012 or higher. I assume that, you already have your development environment properly setup and you already created the Windows Phone 8 project.

 

The next thing that you need to enable the Text-to-Speech engine in your WP8 project is the ID_CAP_SPEECH_RECOGNITION capabilities in the WMAppManifest.xml file.

 

How to enable the ID_CAP_SPEECH_RECOGNITION capability in your WP8 app

 

To enable this capability in your WP8 project, open the Properties folder –> WMAppManifest.xml file and navigate to the Capabilities tab as shown above. There you will find the capability named ID_CAP_SPEECH_RECOGNITION which is needed to enable the Text-to-Speech and Speech Recognition functionalities in your application. Just check it as shown in the above screenshot to enable the same.

 

My Windows Phone can Talk using Text-to-Speech

TTS Code Implementation

Windows Phone 8 SDK provides an API class named SpeechSynthesizer which you can find under the namespace called “Windows.Phone.Speech.Synthesis”. It exposes the following asynchronous methods to handle the Text-to-Speech functionality in your application:

 

public IAsyncAction SpeakTextAsync(string content);
public IAsyncAction SpeakTextAsync(string content, [Variant] object userState);
public IAsyncAction SpeakSsmlAsync(string content);
public IAsyncAction SpeakSsmlAsync(string content, [Variant] object userState);
public IAsyncAction SpeakSsmlFromUriAsync(System.Uri content);
public IAsyncAction SpeakSsmlFromUriAsync(System.Uri content, [Variant] object userState);

 

In this post, we will discuss about the SpeakTextAsync(…) method to let the Windows Phone speak what you want it to say. The rest of the methods are almost similar to that and hence you can easily use them if you understand the basic one.

 

Let’s create one button in the phone page and register the Tap event of the button. As the SpeakTextAsync(…) method is an asynchronous method, we will mark the signature of the event that we registered just now with the keyword async as shown in the below code snippet:

 

private async void OnButtonTapped(object sender, System.Windows.Input.GestureEventArgs e)
{
       var speechSynthesizer = new SpeechSynthesizer();
       await speechSynthesizer.SpeakTextAsync("Hello Reader, Welcome to my blog.");
}

 

Now create an instance of the SpeechSynthesizer class and call the SpeakTextAsync method by passing the text as parameter to it. Make sure to mark the call with await keyword to operate it in asynchronous way.

 

Speech Settings page in Windows Phone 8That’s it! Now build and run your application and tap on the button and listen what your Windows Phone says. That’s the same text that you entered.

 

By default, your phone will speak in the voice that you set in the configuration. So, based on speech settings this will work differently in different user’s device. To change the language and voice in your handset, navigate to Settings –> Speech and modify the values for “Text-to-Speech voice” and “Speech language”. Check how it sounds differently in different language and voice.

 

Remember that, you can not standardize the language and voice in your app. The user can chose the default ones in their handsets and the phone will speak as per that.

 

I hope, the post will help you to understand the Text-to-Speech engine and the process to implement it in your Windows Phone 8 applications or Games. Don’t forget to connect with me on Twitter @kunal2383 and like my Facebook page. Happy Coding!!!


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.

0 comments

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