wolfgang ziegler


„make stuff and blog about it“

Text to Speech - Make your Windows Phone App Talk

August 25, 2013

Adding speech to your Windows Phone app is - like other supposedly advanced tasks - actually really simple as demonstrated by the following code snippet.

private async void Speak()
{
  var synth = new SpeechSynthesizer();    
  await synth.SpeakTextAsync("Hello World of Speech!");
}

One thing to notice though before running this code is that the application’s manifest file has to declare speech capabilities. Otherwise, you will encounter this error.

image

So let’s add this declaration to the manifest file WMAppManifest.xml.

image

Everything works like a charm now and we implemented our first text to speech functionality.

What if my App is not in English?

If your app needs to support languages other than English that’s no big deal either. The SpeechSynthesizer class has a SetVoice method which allows settings different voices for different languages. Depending on the number of installed voices on your phone you can mix and match here as much as you like.

Here’s a code snippet enumerating all installed voices on your Windows Phone and displaying their description in a message box.

var s = string.Empty;
foreach (var voice in InstalledVoices.All)
{
  s += voice.Description;
}
MessageBox.Show(s);

image

So let’s say we want a male German voice - this would be the code to use it in the app.

var maleGermanVoice =
  (from voice in InstalledVoices.All
    where voice.Language == "de-DE" && voice.Gender == VoiceGender.Male
    select voice).FirstOrDefault();

var synth = new SpeechSynthesizer();
synth.SetVoice(maleGermanVoice);
await synth.SpeakTextAsync("Hallo, ich spreche deutsch!");

What about Speech Synthesis Markup Language?

Additionally, the Windows Phone speech synthesizer also supports SSML (Speech Synthesis Markup Language). This is an XML-based descriptive language providing the text to be spoken enhanced with metadata like gender or language. SSML input can be specified directly as a string or from a given file location. Here’s an example specifying an XML string making our Windows Phone app speak Spanish. Me Gusta!

var ssml = "<speak version=\"1.0\" ";
ssml += "xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang=\"es-ES\">";
ssml += "Hola amigo! Que tal?</speak>";

That was easy, right?