在本文中,将探讨如何利用云服务解决文本到语音转换的问题。在 Windows Phone 7 上实现这种功能非常简单,可以使用 Bing API 来实现。以下是如何检索 Microsoft Translator 支持的语言列表并朗读用户输入文本的步骤。
首先,需要获取一个有效的 Bing API AppID,请按照以下步骤操作:
注册应用程序后,将开始开发Windows Phone 7应用程序并调用云服务。
要添加 Microsoft Translator Service 的 Web 引用,需要向 Windows Phone 项目添加服务引用。在解决方案资源管理器中右键单击 Windows Phone 项目,并选择“添加服务引用”。
现在向Windows Phone 7项目添加一个 Panorama 页面。
根据应用程序需求创建 UI,以下是 XAML 代码片段。这里添加了三个 Panorama 项目。
<Grid x:Name="LayoutRoot">
<controls:Panorama Title="text to speech" Name="panoSpeech" Foreground="Blue" FontFamily="Comic Sans MS">
<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>
<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="Speak" Height="90" Margin="0,30,0,0" Name="btnSpeak" Width="338" Click="btnSpeak_Click"/>
</StackPanel>
</controls:PanoramaItem>
<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>
第一个 Panorama 项目用于开发检索支持的语音语言。要检索支持的语言,需要调用 Web 服务方法 “GetLanguagesForSpeakAsync”。GetLanguagesForSpeak 方法仅返回语言代码,例如,"en" 代表英语,"fr" 代表法语等。
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
try
{
FrameworkDispatcher.Update();
var objTranslator = new ServiceReference1.LanguageServiceClient();
objTranslator.GetLanguagesForSpeakCompleted += new EventHandler(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(translator_GetLanguageNamesCompleted);
objTranslator.GetLanguageNamesAsync(AppId, "en", e.Result, e.Result);
}
void translator_GetLanguageNamesCompleted(object sender, GetLanguageNamesCompletedEventArgs e)
{
var codes = e.UserState as ObservableCollection;
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;
});
}
第二个 Panorama 项目用于开发使用 SpeakAsync 方法朗读文本。SpeakAsync 方法接受四个 string 参数,分别是 AppId、SpeechText、SpeechLanguage 和格式。
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));
}