Reader Level:
ARTICLE

Text To Speech in Windows Phone

Posted by Ibrahim Ersoy Articles | Windows Phone December 08, 2012
We'll be using Google's Text To Speech Engine in a Windows Phone 7 Application.
  • 0
  • 0
  • 9172
Download Files:
 

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: http://translate.google.com/translate_tts?tl=en&q=HelloWorld

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:

one.png

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="http://translate.google.com/translate_tts?tl=en&amp;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:

two.png

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"/>
</Grid> 


First add a static variable to store the page URL:

public static string path = "http://translate.google.com/translate_tts?tl=en&q="; 

Then in a button click event write these:

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

Now run it!

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


Everything will be working fine.

Hope that helps!

COMMENT USING

Trending up