Reader Level:

Text To Speech in Windows Phone

By Ibrahim Ersoy on Dec 08, 2012
We'll be using Google's Text To Speech Engine in a Windows Phone 7 Application.

Alright, as you may very well know, you can translate any word using Google Translate. But Google has a secret weapon; a builtin Google Translate.

Normally, you can listen to text spoken using the web page here:

When you view this webpage, you will see a HTML 5 video tag.

If you've tried to use this webpage in a WebBrowser control, this trick won't work. I am also pretty sure that the Internet Explorer in WP7 doesn't support HTML5.

Let's get back to our topic. When you open that website you'll see a video player:


If you're using Google Chrome, when you right-click on that control and click "inspect element" then this is where the Developer Tools will display now.

See the HTML tag there:
<video controls="" autoplay="" name="media" __idm_id__="772538369"> <source src=";q=Hell%20World" type="audio/mpeg"> </video>

It has a source with a type "audio/mpeg" that means we don't need a Webbrowser.

All we need is : MediaElement!

Before we start coding, my page looks like this:


And its XAML:

<Grid x:Name="LayoutRoot" Background="Transparent">
<TextBlock HorizontalAlignment="Left" Margin="109,74,0,0" TextWrapping="Wrap" 
Text="Text To Speech" VerticalAlignment="Top" FontSize="36"/>
<TextBox x:Name="txt1" HorizontalAlignment="Left" Height="72" Margin="10,184,0,0" 
TextWrapping="Wrap" VerticalAlignment="Top" Width="460"/>
<Button Content="Speak!" HorizontalAlignment="Left" Margin="169,256,0,0"
 VerticalAlignment="Top" Click="Button_Click_1"/>
<MediaElement x:Name="med1" HorizontalAlignment="Left" Height="248" 
Margin="10,354,0,0" VerticalAlignment="Top" Width="460"/>

First add a static variable to store the page URL:

public static string path = ""; 

Then in a button click event write these:

med1.Source=new Uri(path + txt1.Text);

Now run it!

Enter something into your Textbox then click the "Speak!" button.

Everything will be working fine.

Hope that helps!

Ibrahim Ersoy
Ibrahim Ersoy

Ibrahim Ersoy helps running and managing C# Corner. He is a Software Consultant in Sharepoint field. He is also interested in Universal Apps, Nodejs, Javascript and mobility. He is also a 2 times C# Corner MVP.