Windows Phone8中实现文本发音(Text to speech)

时间:2021-05-02

编辑和运行Windows Phone应用是件很容易的事情,你只需在微软网站获取一个免费的工具包,在AppHub注册后,并跟着入门教程学习就可以了。

XAML是我的惯用格式。. WP7开发者也许能注意到应用程序标题和页名不再是默认的硬编码,而是通过通过应用的共享资源来实现。通过这种方式,你不必再每次再所有页中设置应用名称。

<phone:PhoneApplicationPage x:Class="TextToSpeechDemo.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True"> <!--LayoutRoot is the root grid where all page content is placed--> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitlePanel contains the name of the application and page title--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock Text="{Binding Path=LocalizedResources.PageTitle, Source={StaticResource LocalizedStrings}}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel> <!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <StackPanel> <ScrollViewer Height="200"> <ComboBox HorizontalAlignment="Left" Width="456" Name="voicesComboBox" DisplayMemberPath="Name" /> </ScrollViewer> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <RadioButton Content="Male" IsChecked="true" Name="MaleRadioButton"/> <RadioButton Content="Female"/> </StackPanel> <TextBox HorizontalAlignment="Left" Height="230" TextWrapping="Wrap" Width="456" Text="I may be a sorry case, but I don't write jokes in base 13." Name="inputTextBox"/> <Button Content="Speak to me" HorizontalAlignment="Left" Width="456" Click="SpeakToMe_Click"/> </StackPanel> </Grid> </Grid></phone:PhoneApplicationPage>

下面是.xaml主页的代码文件。其内容大部分借用自WP8 SDK的文档。我加入了一些错误检测的内容以及性别选择和发音语言选择的功能。

using System;using System.Linq;using System.Windows;using Microsoft.Phone.Controls;using Windows.Phone.Speech.Synthesis; namespace TextToSpeechDemo{ public partial class MainPage : PhoneApplicationPage { SpeechSynthesizer synth; // Constructor public MainPage() { InitializeComponent(); voicesComboBox.ItemsSource = new MyLocals().Items(); } private async void SpeakToMe_Click(object sender, RoutedEventArgs e) { if (voicesComboBox.SelectedIndex == -1) { MessageBox.Show("Please select a language."); } else { if (string.IsNullOrEmpty(inputTextBox.Text)) { MessageBox.Show("Please enter some text."); } else { try { // Initialize the SpeechSynthesizer object. synth = new SpeechSynthesizer(); var myLocal = (MyLocale)voicesComboBox.SelectedItem; // Query for a voice. Results rdered by Gender to ensure the order always goes Female then Male. var voices = (from voice in InstalledVoices.All where voice.Language == myLocal.Lcid select voice).OrderByDescending(v => v.Gender); // gender: 0 = Female, 1 = Male. Corresponds to the index of the above results. int gender = 0; if (MaleRadioButton.IsChecked == true) gender = 1; else gender = 0; // Set the voice as identified by the query. synth.SetVoice(voices.ElementAt(gender)); // Speak await synth.SpeakTextAsync(inputTextBox.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } }}

下面两个类是用来填充发音语言的下拉列表框和检测安装的声音。 里面包含30项(15种语言 x 2种声音)。 通过LINQ查询可以获取所需的声音信息对象,这里可以给出12种语言,而另外3种仍需深入了解。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace TextToSpeechDemo{ class MyLocale { public MyLocale(string name, string lcid) { _name = name; _lcid = lcid; } private string _name; public string Name { get { return _name; } set { _name = value; } } private string _lcid; public string Lcid { get { return _lcid; } set { _lcid = value; } } } class MyLocals { private IList<MyLocale> _myLocals; public MyLocals() { _myLocals = new List<MyLocale>(); _myLocals.Add(new MyLocale("Chinese Simplified (PRC)", "zh-CN")); _myLocals.Add(new MyLocale("Chinese Traditional (Taiwan)", "zh-TW")); _myLocals.Add(new MyLocale("English (United States)", "en-US")); _myLocals.Add(new MyLocale("English (United Kingdom)", "en-GB")); _myLocals.Add(new MyLocale("French (France)", "fr-FR")); _myLocals.Add(new MyLocale("German (Germany)", "de-DE")); _myLocals.Add(new MyLocale("Italian (Italy)", "it-IT")); _myLocals.Add(new MyLocale("Japanese (Japan)", "ja-JP")); _myLocals.Add(new MyLocale("Polish (Poland)", "pl-PL")); _myLocals.Add(new MyLocale("Portuguese (Brazil)", "pt-BR")); _myLocals.Add(new MyLocale("Russian (Russia)", "ru-RU")); _myLocals.Add(new MyLocale("Spanish (Spain)", "es-ES")); } public IEnumerable<MyLocale> Items() { return (IEnumerable<MyLocale>)_myLocals; } }}

接下来的事情是设置 ID_CAP_SPEECH_RECOGNITION功能,否则将抛出异常。

现在,我们如果运行应用将得到下面的结果,选择一种语言并单击按钮,则这个仿真器会与你交谈。

我注意到一件事:如果我选择一个非英语语言而输入英语文本的话,则其发音带有口音。当我将文本设置为发育的数字文本:1, 2, 3, 4,同时将语言设置为与法语相似的语言(如西班牙语),则其对“4”的发音是西班牙语,而非法语,这听起来很有趣。

本文说明:

本文翻译的国外作者的一篇文章,在翻译时省略了一些内容,由于水平有限,个别内容获取翻译不够准备却,敬请给出意见或建议,原文地址:http://mobile.dzone.com/articles/text-speech-windows-phone-8

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章