Windows Phone 8.1 TTS Voice API - What’s changed for #WPDev?


In the last post “Windows Phone 8.1 Text-To-Speech - What’s changed for #WPDev?”, we discussed about the Text-To-Speech API changes in Windows Phone 8.1 in comparison to Windows Phone 8.0 APIs. We also covered the capability changes in WP8.1.

 

Today in this blog post we will learn about the API changes to retrieve all voices installed in the device as well as the way to set a specific voice. Continue to read further.

 

You might already know that, many APIs have been changed in Windows Phone 8.1 if you compare them with it’s previous version (i.e. WP8). I have already blogged few of them. You can read them here:

 

How to retrieve all installed voices in Windows Phone 8.0

Prior to Windows Phone 8.1, we had a static property named “InstalledVoices.All”, which was actually returning us the list of all the installed voices in the user’s device like this:

 

var installedVoices = InstalledVoices.All;

 

If you are working on Windows Phone 8.1 now, you will not find this. Rather you have a different property in a different class.

 

How to retrieve all installed voices in Windows Phone 8.1

In WP 8.1, you have to call the static property named “AllVoices” which is part of a sealed class named “SpeechSynthesizer” present under the namespace “Windows.Media.SpeechSynthesis.SpeechSynthesizer”. Here is a code snippet for your reference:

 

var installedVoices = SpeechSynthesizer.AllVoices;

 

Once you have the list of all installed voices, you can write your own custom logic to meet your business requirement.

 

How to retrieve the current voice in Windows Phone 8.0

In Windows Phone 8.0, you had to call the GetVoice() method of the instance of the SpeechSynthesizer class to retrieve the current voice:

 

using (var speechSynthesizer = new SpeechSynthesizer())
{
     var currentVoice = speechSynthesizer.GetVoice();
}

 

How to retrieve the current voice in Windows Phone 8.1

This has been changed a little in Windows Phone 8.1. Now instead of calling the method, we have a property called “Voice”, which returns the current voice:

 

using (var speechSynthesizer = new SpeechSynthesizer())
{
     var currentVoice = speechSynthesizer.Voice;
}

 

You can observe in the above code that the class is still same but the method has been changed to a property. Once you have the current voice, you can set the voice of Male or Female to read out the entered text. Let’s see how we can implement it.

 

How to set a new voice in Windows Phone 8.0

Looking to set the gender of the voice in Windows Phone 8.0? You could do it easily by calling the SetVoice(voice) method by passing the specific voice of gender tone of your choice. You can see the example code snippet below:

 

using (var speechSynthesizer = new SpeechSynthesizer())
{
     speechSynthesizer.SetVoice(InstalledVoices.All.First(v => v.Gender == VoiceGender.Female));
}

 

Notice that, here we called the InstalledVoices.All to get the list of all installed voices and then we filtered out the first voice of gender type Female. You can also change the above code to set the current voice of specific gender (i.e Male or Female).

 

How to set a new voice in Windows Phone 8.1

Again, this has been changed a bit in Windows Phone 8.1. Now, instead of calling speechSynthesizer.SetVoice(voice) method, you have to call speechSynthesizer.Voice property and set the voice of male or female. Here too, you can get a voice of specific language and set it as the current voice.

 

using (var speechSynthesizer = new SpeechSynthesizer())
{
     speechSynthesizer.Voice = SpeechSynthesizer.AllVoices.First(v => v.Gender == 
                                                                 VoiceGender.Female);
}

 

Remember that, it’s not only the gender but you can also set a voice of various languages like French, Chinese etc. Checkout the various properties available in the voice instance.

 

End Note

I hope, this blog post will give you the basic idea to start with Text-to-Speech (TTS) engine in Windows Phone 8.1. In case you are looking to upgrade your existing app to WP 8.1, this will guide you to change the API calls easily without pulling your hair.

 

Don’t forget to share it in your network to spread the content and help the other developer to find it easily. Drop a line below with your queries, feedback and suggestions if any. I would be very happy if you do so.

 

Connect with me on Twitter and Facebook to get all the updates in your feed. Subscribe to my blog’s RSS feed and Email Newsletter to get the new article notifications in your inbox. We will neither spam your inbox nor share your email details with anybody.


 


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.

1 comments


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