Share via


Windows Phone 8.1 for Developers - Text to speech

This blog post is part of a series about how Windows Phone 8.1 affects developers. This blog post talks about how we can use the text-to-speech functionality and is written by Alexander Persson at Jayway and was originally posted here.

This blog post is part of a series about how Windows Phone 8.1 affects developers. The series is written in collaboration with Microsoft evangelist Peter Bryntesson, check out his blog here.

Introduction

Using Text to speech (TTS) in your app can make it stand out from the crowd as a cool feature. But it can also come in handy when you want to make your app accessible for more people that has a hard time reading the often small fonts on the screen.

Speak text

In Windows Phone 8.0 it was really easy to get TTS to work. You just added a few lines of code and it was all set:

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

This code will just take your parameter and speak the text. You had to turn on Speech recognition capability in your app manifest or you would get an Access denied. But overall it’s not that much code. In Windows Phone 8.1 we have a new API that works like it does in Windows Store apps. Notice we have to enable Microphone capability.

         private async Task TextToSpeach(string textToRead)
        {
            using (var speech = new SpeechSynthesizer())
            {
                var voiceStream = await speech.SynthesizeTextToStreamAsync(textToRead);
                player.SetSource(voiceStream, voiceStream.ContentType);
                player.Play();
            }
        }

A little bit more code and another namespace than in Windows Phone 8.0. Before we are able to speak our text we must make it into a stream. This stream do we then set as source in our MediaElement (named as player in XAML) before we can play the text. So a few more lines and we now also require a MediaElement to be able to play the text. The nice with this is that now we get a stream and we can do so much fun with a stream. It’s possible to save to disk, share to another player app and use in some awesome audio mixer application.

 

Get all voices installed

As a result of new namespaces the API for getting the installed voices has also changed. In Windows Phone 8.0 we had:

 var voices = InstalledVoices.All;

And in Windows Phone 8.1 we have:

 var voices = SpeechSyntheizer.AllVoices;

Both return an IReadOnlyList of VoiceInformation, so the result is the same on both platforms.

 

Getting voice and set a new one

It’s possible to get the current voice we are using and it’s done similar on both platforms. In Windows Phone 8.0:

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

In Windows Phone 8.1:

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

To set a new voice, in this example the first female voice. In Windows Phone 8.0:

             using (var speech = new SpeechSynthesizer())
            {
                speech.SetVoice(InstalledVoice.All
                    .First(i => i.Gender == VoiceGender.Female));
            }

And in Windows Phone 8.1:

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

  

Summary

This post described some of the new features of text to speech and how the API changed from Windows Phone 8.0. As we could see the API hadn’t change that much besides how we did play the text aloud. In Windows Phone 8.1 we now require a MediaElement but we benefit from getting a stream we can do whatever we want to.

Comments

  • Anonymous
    May 17, 2014
    Please tell me WP 8.1 added the ability to run TTS while background audio is playing...

  • Anonymous
    July 14, 2014
    I tried TTS in a test app and it would not work. I copied the code from your article, enable Microphone capability and added a MediaElement. It failed to talk on the 8.1 emulator. ( I can get TTS to work on 8.0 emulator) I tried playing around with the code but it will not work for me. Is there a problem with TTS on 8.1 or is it my coding/setup ?? ( I am using VS 2013 Ultimate update 2. I am coding a  (not a Silverlight) 8.1 app) Apart from my problem, this is a useful article. There is no example of TTS in the 8.1 samples from MS. David