麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 課堂 > 技術(shù)開發(fā) > 正文

Windows Phone8中實現(xiàn)文本發(fā)音(Text to speech)

2023-06-15 12:14:55
字體:
供稿:網(wǎng)友

編輯和運行Windows Phone應(yīng)用是件很容易的事情,你只需在微軟網(wǎng)站獲取一個免費的工具包,在AppHub注冊后,并跟著入門教程學(xué)習(xí)就可以了。

XAML是我的慣用格式。. WP7開發(fā)者也許能注意到應(yīng)用程序標(biāo)題和頁名不再是默認(rèn)的硬編碼,而是通過通過應(yīng)用的共享資源來實現(xiàn)。通過這種方式,你不必再每次再所有頁中設(shè)置應(yīng)用名稱。

<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主頁的代碼文件。其內(nèi)容大部分借用自WP8 SDK的文檔。我加入了一些錯誤檢測的內(nèi)容以及性別選擇和發(fā)音語言選擇的功能。

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

下面兩個類是用來填充發(fā)音語言的下拉列表框和檢測安裝的聲音。 里面包含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;        }    }}

接下來的事情是設(shè)置 ID_CAP_SPEECH_RECOGNITION功能,否則將拋出異常。

現(xiàn)在,我們?nèi)绻\行應(yīng)用將得到下面的結(jié)果,選擇一種語言并單擊按鈕,則這個仿真器會與你交談。

我注意到一件事:如果我選擇一個非英語語言而輸入英語文本的話,則其發(fā)音帶有口音。當(dāng)我將文本設(shè)置為發(fā)育的數(shù)字文本:1, 2, 3, 4,同時將語言設(shè)置為與法語相似的語言(如西班牙語),則其對“4”的發(fā)音是西班牙語,而非法語,這聽起來很有趣。

本文說明:

本文翻譯的國外作者的一篇文章,在翻譯時省略了一些內(nèi)容,由于水平有限,個別內(nèi)容獲取翻譯不夠準(zhǔn)備卻,敬請給出意見或建議。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 免看黄大片aa | 92看片淫黄大片欧美看国产片 | 久久综合给合久久狠狠狠97色69 | 羞羞视频免费网站含羞草 | 中国黄色一级生活片 | 成人午夜看片 | 美国黄色毛片女人性生活片 | 黄色特级 | 伊人久操视频 | 日韩黄色三级视频 | 中国av中文字幕 | 在线视频观看国产 | 欧洲成人在线视频 | 黄色影院网站 | 永久av在线免费观看 | 在线成人免费观看视频 | 国产99视频精品免视看9 | 黄色毛片前黄 | 色av成人天堂桃色av | 亚洲天堂在线电影 | 黄色大片免费看 | 久久中文免费 | 国产69精品福利视频 | 久草在线资源视频 | 久久久大片| 中文字幕视频在线播放 | 97人人草| 国产免费一区二区三区网站免费 | 2017亚洲男人天堂 | 亚洲午夜久久久精品一区二区三区 | 欧美激情猛片xxxⅹ大3 | 久久久久久久久久91 | 黄色av片在线观看 | 欧美午夜网| 国产精品久久久久久久久久妇女 | 国产一级aaa全黄毛片 | 中文字幕在线播放视频 | 性爱在线免费视频 | 日本网站一区二区三区 | 日本aaaa片毛片免费观看视频 | 国产成人综合在线观看 |