ARTICLE

Text to Speech in Windows Phone 7

Posted by Pavan Pareta Articles | Windows Phone 8 March 31, 2012
How to use the Text to Speech API in Windows Phone 7.
Reader Level:
Download Files:
 

Introduction

In this article I am explaining how to leverage the 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 the Bing API. Here I will show how we can retrieve a list of languages supported by the 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 use the following steps.

Step 1 : Open the following Url to register your application and follow the instructions to obtain a valid Bing API AppID.

http://www.bing.com/developers/appids.aspx

1.png

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

2_small.png

After having registered your application, we will proceed with the Windows Phone 7 application development and invoke the cloud service.

Step 3 : Create a Windows Phone 7 application project.

3.png

Step 4 :
 To add a web reference of the Microsoft Translator Service, we need to add a service reference to the Windows Phone project. Right-click the Windows Phone Project in the Solution Explorer, and choose Add Service Reference. Please see the following pictures of that.

http://api.microsofttranslator.com/V2/Soap.svc

image step 4.gif

5.png

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

step 6.png

Step 6 : Create a UI as per application requirements; see the following XAML code snippet. Here I have added three panorama items.

<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" FontSize="72">
            <StackPanel Orientation="Horizontal">
                <StackPanel.Resources>
                    <DataTemplate x:Key="LanguageTemplate">
                        <TextBlock Foreground="White" Margin="0,0,0,0" Text="{Binding Name}"  />
                    </DataTemplate>
                </StackPanel.Resources>
                <ListBox HorizontalAlignment="Left" ItemTemplate="{StaticResource LanguageTemplate}" Margin="20,10,0,20" Name="ListLanguages" Width="441">
                </ListBox>
            </StackPanel>
        </controls:PanoramaItem>
        <!--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 proffesional. 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" />
            </StackPanel>
        </controls:PanoramaItem>
        <!--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" />
            </StackPanel>
        </controls:PanoramaItem>
    </controls:Panorama>
</
Grid>

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

7 1.png

GetLanguagesForSpeakAsync takes two methods like AppID and object.


 void MainPage_Loaded(object sender, RoutedEventArgs e)
 {
          try
         {
              FrameworkDispatcher.Update();
              var objTranslator = new ServiceReference1.LanguageServiceClient();
             objTranslator.GetLanguagesForSpeakCompleted += new EventHandler<GetLanguagesForSpeakCompletedEventArgs>(translator_GetLanguagesForSpeakCompleted);
              objTranslator.GetLanguagesForSpeakAsync(AppId, objTranslator);
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
     void translator_GetLanguagesForSpeakCompleted(object sender, GetLanguagesForSpeakCompletedEventArgs e)
      {
          var objTranslator = e.UserState as ServiceReference1.LanguageServiceClient;
          objTranslator.GetLanguageNamesCompleted += new EventHandler<GetLanguageNamesCompletedEventArgs>(translator_GetLanguageNamesCompleted);
          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
                           }).ToArray();
          this.Dispatcher.BeginInvoke(() =>
          {
              this.ListLanguages.ItemsSource = languagesData;
          });
      }

 

    }

}


Step 8 : The second Panorama item used in development for speaking the text using the SpeakAsync method takes four string parameters like AppId, SpeechText, SpeechLanguage, format. See the following UI and code snippet.

step 8 image.gif

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);
        se.Play();
    });
    client.OpenReadAsync(new Uri(e.Result));
}

Step 9 : Now build the application and execute it.

out 1.gif

out 2.gif

out 3.gif

Thank you for your time.

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

hi, I have registered my application in WINDOWS AZURE MARKETPLACE, and i can get the client id, but not the "APP ID", where and how can get that "APP ID".

Posted by noorul hasan Apr 15, 2013

Thanks Mahesh for your nice comments, Actually the idea came when I was trying to developed this application for blind peoples, so this API can help to read their SMS, unfortunately WP7 SDK does not supports sms access API.

Posted by Pavan Pareta Apr 03, 2012

Good work Pavan. Wonder if Silverlight supports TTS or not. WPF can use Speech API but I wasn't sure about Silverlight. If SL does and we build WP application using SL, then we probably won't need translator service. Just curious.

Posted by Mahesh Chand Apr 02, 2012

It's a good start Pavan having nice concept. Well keep it up and thanks for sharing....

Posted by Amit Maheshwari Apr 01, 2012

Very nice article ...thanks for sharing

Posted by Arjun Panwar Apr 01, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
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.