Click here to Skip to main content
15,354,545 members
Articles / Mobile Apps / Windows Phone 7
Posted 25 Sep 2011


29 bookmarked

Text to Speech in Windows Phone 7

Rate me:
Please Sign up or sign in to vote.
4.83/5 (14 votes)
23 Nov 2011CPOL2 min read
How to use the Text to Speech API in Windows Phone 7


This article discusses how to use Cloud-based Microsoft Translator Service in Windows phone 7 to translate text to speech.  


In this article, I am going to explain how we can take the leverage of cloud to solve the problem of Text to Speech translation. It’s pretty simple to archive such kind of functionality in Windows Phone 7 using Bing API. Here I will show how we can retrieve a list of languages supported by Microsoft Translator for the Speech API and speak the user’s input text.

First of all, we must obtain a valid Bing API AppID, let's follow the below steps.

Step 1: Open the below mentioned URL to register your application, and follow the instructions to obtain a valid Bing API AppID.

1.png - Click to enlarge image

Step 2: Enter required information and obtain a valid Bing API AppID.

2.png - Click to enlarge image 

Once you register your application, now we will be moving ahead with the Windows phone 7 application developments and invoke the cloud service.

Step 3: Create a Windows phone 7 application project:

WP_001.png - Click to enlarge image

Step 4: To add web reference of the Microsoft Translator Service, we need to add a service reference to Windows Phone project. Right click the Windows Phone Project in solution explorer, and choose Add Service Reference. Please see the below pictures for the same.




Step 5: Now add a panorama page to Windows phone 7 project.

WP_005.png - Click to enlarge image

Step 6: Create a UI as per application requirement, see below XAML code snippet. Here I have added three panorama items. 

Using the XAML Code for UI Construction

<Grid x:Name="LayoutRoot">
    <controls:Panorama Title="text to speech" Name="panoSpeech" 
           Foreground="Blue" FontFamily="Comic Sans MS">
        <!--Panorama item one-->
        <controls:PanoramaItem Header="Language(s)" 
               Foreground="Plum" FontFamily="DengXian" 
            <StackPanel Orientation="Horizontal">
                    <DataTemplate x:Key="LanguageTemplate">
                        <TextBlock Foreground="White" 
                           Margin="0,0,0,0" Text="{Binding Name}"  />
                    <ListBox HorizontalAlignment="Left" 
                       ItemTemplate="{StaticResource LanguageTemplate}" 
                       Name="ListLanguages" Width="441">

        <!--Panorama item two-->
        <controls:PanoramaItem Header="Speech" Foreground="Yellow">
            <StackPanel Orientation="Vertical" Margin="20,0,0,0">
                <TextBox Name="TextToSpeachText" 
                   Text="This Pavan Pareta, Microsoft Most Value able professional. 
                         He has written an application for windows phone 7" 
                   TextWrapping="Wrap" Height="350" />
                <Button Content="S p e a k" Height="90" 
                   Margin="0,30,0,0" Name="btnSpeak" 
                   Width="338" Click="btnSpeak_Click" />

        <!--Panorama item three-->
        <controls:PanoramaItem Header="Speak" Foreground="Violet">
            <StackPanel Orientation="Vertical">
                <Image Height="auto" Name="image1" 
                   Stretch="None" Width="auto" 
                   Margin="50 60 80 0" Source="/speak.jpg" />

Step 7: First Panorama item used to develop for retrieving supported speech languages. To retrieve the supported language, we need to call web service method “GetLanguagesForSpeakAsync”. The GetLanguagesForSpeak method only returns the language codes, for example, en for English and fr for French, etc. See the below UI and code snippet.


GetLanguagesForSpeakAsync takes two methods like AppID and object

void MainPage_Loaded(object sender, RoutedEventArgs e)
        var objTranslator = new ServiceReference1.LanguageServiceClient();
        objTranslator.GetLanguagesForSpeakCompleted += 
          new EventHandler<GetLanguagesForSpeakCompletedEventArgs>(
        objTranslator.GetLanguagesForSpeakAsync(AppId, objTranslator);
    catch (Exception ex)

void translator_GetLanguagesForSpeakCompleted(object sender, 
                GetLanguagesForSpeakCompletedEventArgs e)
    var objTranslator = e.UserState as ServiceReference1.LanguageServiceClient;
    objTranslator.GetLanguageNamesCompleted += 
      new EventHandler<GetLanguageNamesCompletedEventArgs>(
    objTranslator.GetLanguageNamesAsync(AppId, "en", e.Result, e.Result);

void translator_GetLanguageNamesCompleted(object sender, 
     GetLanguageNamesCompletedEventArgs e)
    var codes = e.UserState as ObservableCollection<string>;
    var names = e.Result;
    var languagesData = (from code in codes
                     let cindex = codes.IndexOf(code)
                     from name in names
                     let nindex = names.IndexOf(name)
                     where cindex == nindex
                     select new TranslatorLanguage()
                         Name = name,
                         Code = code
    this.Dispatcher.BeginInvoke(() =>
        this.ListLanguages.ItemsSource = languagesData;

Step 8: Second Panorama item used to develop for speak text using SpeakAsync method takes four string parameters like AppId, SpeechText, SpeechLanguage, format. See the below UI and code snippet.


private void btnSpeak_Click(object sender, RoutedEventArgs e)
    var languageCode = "en";
    var language = this.ListLanguages.SelectedItem as TranslatorLanguage;
    if (language != null)
        languageCode = language.Code;
    var objTranslator = new ServiceReference1.LanguageServiceClient();
    objTranslator.SpeakCompleted += translator_SpeakCompleted;
    objTranslator.SpeakAsync(AppId, this.TextToSpeachText.Text, 
                             languageCode, "audio/wav");

    panoSpeech.DefaultItem = panoSpeech.Items[(int)2];            

void translator_SpeakCompleted(object sender, ServiceReference1.SpeakCompletedEventArgs e)
    var client = new WebClient();
    client.OpenReadCompleted += ((s, args) =>
        SoundEffect se = SoundEffect.FromStream(args.Result);
            client.OpenReadAsync(new Uri(e.Result));

Step 9: Now build the application and execute it.  




Points of Interest

FrameworkDispatcher.Update(), Bing API which allows speech to written text. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) WmDev Technology
India India
Pavan is a Senior software developer, and has been in the industry since August 2005. He has experience in Windows Phone 7, Windows Mobile with Compact Framework, Python for S60, C#.NET, VB.NET, Windows Forms, ASP.NET, WCF, Silverlight, JavaScript and HTML.

Comments and Discussions

GeneralVoice to text convertion Pin
Member 1124959030-Nov-14 23:55
MemberMember 1124959030-Nov-14 23:55 
QuestionSpeechSynthesizer() difficulty Pin
Member 1014337910-Jul-13 2:15
MemberMember 1014337910-Jul-13 2:15 
QuestionSend it to Email Too? Pin
alan933-Apr-13 14:12
Memberalan933-Apr-13 14:12 
QuestionHi.About Credential Of Web Service Pin
Member 395610926-Mar-13 7:19
MemberMember 395610926-Mar-13 7:19 
QuestionText To Speech with problem Pin
@Giovanni__jr2-Oct-12 7:56
Member@Giovanni__jr2-Oct-12 7:56 
QuestionNew MS Translator Format Pin
Bowo Fadoju7-Aug-12 17:30
MemberBowo Fadoju7-Aug-12 17:30 
NewsLooks like the Bing APIs are now Deprecated and thus you can't use this Pin
Kunal Chowdhury «IN»13-Jun-12 22:13
professionalKunal Chowdhury «IN»13-Jun-12 22:13 
QuestionNeed help! Pin
zenith.tav13-May-12 17:55
Memberzenith.tav13-May-12 17:55 
AnswerRe: Need help! Pin
renaud_25623-May-12 12:37
Memberrenaud_25623-May-12 12:37 
GeneralMy vote of 1 Pin
Abdullatif M. Abu Al Rub7-May-12 22:05
MemberAbdullatif M. Abu Al Rub7-May-12 22:05 
QuestionBing APIs are deprecated. Any help Pin
Santiago Sanchez28-Apr-12 7:18
MemberSantiago Sanchez28-Apr-12 7:18 
QuestionRecord possible Pin
yogarajan_Ganesan20-Feb-12 21:50
Memberyogarajan_Ganesan20-Feb-12 21:50 
GeneralMy vote of 5 Pin
Santiago Sanchez22-Dec-11 10:30
MemberSantiago Sanchez22-Dec-11 10:30 
GeneralMy vote of 5 Pin
Nji, Klaus6-Dec-11 0:35
MemberNji, Klaus6-Dec-11 0:35 
GeneralRe: My vote of 5 Pin
PavanPareta14-Dec-11 0:45
MemberPavanPareta14-Dec-11 0:45 
GeneralMy vote of 5 Pin
Nasenbaaer5-Dec-11 21:21
MemberNasenbaaer5-Dec-11 21:21 
GeneralMy vote of 5 Pin
Steve Maier6-Oct-11 8:46
professionalSteve Maier6-Oct-11 8:46 
GeneralMy vote of 5 Pin
tystent26-Sep-11 12:45
Membertystent26-Sep-11 12:45 
GeneralThanks Pin
Ahmed Farag Ibrahim25-Sep-11 4:48
MemberAhmed Farag Ibrahim25-Sep-11 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.