Home > C# | Windows Phone > Windows Phone text to speech

Windows Phone text to speech

In Windows Phone there are great features to communicate with an app using voice. In previous post I have described how to command your app using your voice. In this post I will cover on how the app can respond using text to speech. The feature text to speech can be used in various scenarios to give the user feedback and is definitely a great tool to optimize your apps accessibility. The entire source code can be downloaded at the end of the post.

Windows Phone have support for 15 languages (sadly, not Swedish) and each language comes with two voices, one male and one female. The phone has one voice set as default. This can be viewed and changed in the phone settings. The default voice is the one to be used if not the app say otherwise.

windows phone settings

windows phone settings
speech settings

speech settings

First of all we create a simple UI with three buttons supporting our three scenarios. Add event handlers to the buttons.

main ui

main ui

Now we need to add speech recognition capability to the app. If not, we will get a security error when we try to speak any text. Open the app manifest and add the capability as shown in the picture below.

Speech recognition capability

Speech recognition capability

Now we will dig into the code and start with scenario number 1. The crazy thing here is that only two lines of code are needed to speech text with the default voice. First of all we create a new method and in that we create a SpeechSynthesizer object that will be used to speak the text we will provide for it. Then we will use the method SpeakTextAsync on the synthesizer-object and send in the text. Note that this method is async so we need to decorate our method with the async keyword.

 
        private async void GreetInEnglish()
        {
            //Fist create the synth that will be playing the sound
            SpeechSynthesizer synth = new SpeechSynthesizer();
            //await the synth while it speaks the text of your choice. This will use the default voice.
            await synth.SpeakTextAsync("Hello man! How are you today? Well i'm just fine myself!");
        }

Run the app and hear it greet you! :)

Now for scenario no 2 we will start out as in scenario 1 with a new method. Now we will tell the phone to use a female French voice. This is easily done by querying the InstalledVoices.All for a voice that is of culture “fr-FR” and female. Then we will tell the SpeechSynthesizer-object to use that voice. I have used Google-translate to translate the text from the previous sample to French. This will make the speech sound more natural J

 
        private async void GreetInFrench()
        {
            //Create the synth that will be playing the sound
            SpeechSynthesizer synth = new SpeechSynthesizer();

            //Find the voice of your choice. In this case french woman
            var voice = (from x in InstalledVoices.All
                     where x.Language == "fr-FR" &&
                     x.Gender == VoiceGender.Female
                     select x).FirstOrDefault();

            //Tell the synth to use the selected voice
            synth.SetVoice(voice);

            //await the synth while it speaks the text of your choice with the selected voice
            await synth.SpeakTextAsync("Bonjour homme! Comment allez-vous aujourd'hui? Eh bien, je suis juste moi-même très bien!");
        }

For our final scenario we will create a method that will loop through all the installed voices and let them say “Hello World”. Just as in the previous example we will use the installed voices, but this time we will loop over them and let each and one of them speak the text.

 
        private async void LoopAllVoices()
        {
            //Create the synth that will be playing the sound
            SpeechSynthesizer synth = new SpeechSynthesizer();

            //Loop all the installed voices
            foreach (var voice in InstalledVoices.All)
            {
                //Tell the synth to use the selected voice
                synth.SetVoice(voice);
                //await the synth while it speaks the text with the current voice
                await synth.SpeakTextAsync("Hello world!");
            }
        }

Download the source code and have fun! :)

download source code

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackbacks:0

Listed below are links to weblogs that reference
Windows Phone text to speech from Bjarneryd.se
TOP