在 Windows Phone 7 编写 TTS 程序 (Text to Speech)

本文将介绍如何在 Windows Phone 7 手机上使用微软的翻译服务。这将会用到微软的 Speech API 。

下面是详细的步骤:

1. 首先我们要申请一个 AppID

访问网址:http://www.bing.com/developers/appids.aspx

2. 输入必须的信息然后获取可用的 Bind API AppID

一旦注册成功,我们就可以开始 Windows Phone 7 上的程序开发了。

3. 创建一个 Windows Phone 7 的项目

4. 添加对微软翻译服务的 Web 应用,首先需要添加服务引用到项目中。右击项目并选择“ Add Service Reference”,如下图所示:

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

5. 现在添加一个全景页到项目中

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

7. 接下来需要调用 Web 服务方法:“GetLanguagesForSpeakAsync”. 该方法只返回语言代码,例如 English 会返回 en,而 French 返回 fr,界面和代码如下所示

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;
    });
}

8. 第二个界面用来输入要发音的文本:

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));

9. 接下来就是编译项目并执行了

英文原文:http://www.codeproject.com/KB/windows-phone-7/Text_to_speech_in_wp7.aspx

posted @ 2011-12-16 11:56  风花雪月  阅读(499)  评论(0编辑  收藏  举报