ARTICLE

Text To Speech in Windows Phone

Posted by Ibrahim Ersoy Articles | Windows Phone 8 December 08, 2012
We'll be using Google's Text To Speech Engine in a Windows Phone 7 Application.
Reader Level:
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!

Login to add your contents and source code to this article
post comment
     

@Sam: Thank you! Yes according to the article we can use HTML5 in internet explorer at Mango update.i didnt know that.But playing a sound from a webbrowser control would be a bad idea.

Posted by Ibrahim Ersoy Jan 09, 2013

your welcome yusuf!

Posted by Ibrahim Ersoy Jan 09, 2013

Good job ibo !

Posted by Yusuf Karatoprak Jan 08, 2013

Does http://msdn.microsoft.com/en-us/windowsmobile/hh369944.aspx help? It is called "How Do I: Use HTML5 in the WebBrowser Control in Windows Phone ´Mango´?". Also, for Desktop PCs at least we need to specify that a web page uses IE9 features.

Posted by Sam Hobbs Jan 05, 2013

Got it.

Posted by Mahesh Chand Dec 09, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.