Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
在Windows8.1 没有发布之前,也就是在Windows 8版本的时代,很多人会问到这样一个问题即如何在windows store app程序中将文本转换成语音形式。很遗憾的是在Windows 8版本中,并没有提供一个直接实现此功能的API,且.Net 里面的System.Speech 类等一系列的API在windows 8 store app中并不被支持。我们会建议在程序中使用Bing Translator Service或者寻求第三方的API如Google TTS。无论是以上任何一种方式,实现都会显得过于复杂,且需要开发者花费一段时间来熟悉和掌握第三方的API。
随着windows 8.1 版本的发布,这个问题已经迎刃而解了,您仅仅需要几行代码就可以使您的程序开口说话。
您需要一个新的API:Windows.Media.SpeechSynthesis namespace。在这个命名空间下有三个类,即SpeechSynthesisStream,SpeechSynthesizer和VoiceInformation。其中SpeechSynthesizer提供了对已安装的语音合成引擎的功能的访问。SpeechSynthesisStream可以对音频流数据进行读写操作。VoiceInformation可以获得已安装的语音的信息。 利用这三个类, 您就可以在windows store app中来让您的程序开口说话。
下面有一个简单的例子,该例子可以朗读指定的文本以及朗读SSML文档。
以下是示例项目的界面:
其中在该页面添加了一个MediaElement控件,用它来播放语音流:
<MediaElement Name="media" Grid.Row="0"/>
点击LoadText按钮可以打开一个存储在电脑里面的Text文件:
点击SpeakText按钮即可以朗读该文本文件里面的文字内容,实现代码如下:
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync(textbox.Text.ToString());
this.media.AutoPlay = true;
this.media.SetSource(stream, stream.ContentType);
this.media.Play();
}
其中synth是指已经定义的SpeechSynthesizer类:
SpeechSynthesizer synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
SynthesizeTextToStreamAsync方法是将文本内容转化为音频流。
界面中Combobox的列表显示的是本地已安装的语音列表,即通过VoiceInformation可以获得该列表:
private void VoiceChooser_Initialize()
{
// get all of the installed voices
var voices = Windows.Media.SpeechSynthesis.SpeechSynthesizer.AllVoices;
// get the currently selected voice
VoiceInformation currentVoice = this.synth.Voice;
foreach (VoiceInformation voice in voices)
{
ComboBoxItem item = new ComboBoxItem();
item.Name = voice.DisplayName;
item.Tag = voice;
item.Content = voice.DisplayName;
this.VoiceChooser.Items.Add(item);
// check to see if this is the current voice, so that we can set it to be selected
if (currentVoice.Id == voice.Id)
{
item.IsSelected = true;
this.VoiceChooser.SelectedItem = item;
}
}
}
用户通过选择不同的Combobox Item,可以用不同的语音形式来朗读该文本:
private void VoiceChooser_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem item = (ComboBoxItem)this.VoiceChooser.SelectedItem;
VoiceInformation voice = (VoiceInformation)item.Tag;
this.synth.Voice = voice;
}
点击LoadSSML按钮可以显示一个SSML文件的内容:
点击SpeakSSML文件可以朗读SSML文件:
private async void speakssml_Click(object sender, RoutedEventArgs e)
{
SpeechSynthesisStream stream = await synth.SynthesizeSsmlToStreamAsync(textbox.Text.ToString());
this.media.AutoPlay = true;
this.media.SetSource(stream, stream.ContentType);
this.media.Play();
}
SSML文件如下:
<?xml version="1.0"?>
<speak version="1.0" xmlns="https://www.w3.org/2001/10/synthesis"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.w3.org/2001/10/synthesis
https://www.w3.org/TR/speech-synthesis/synthesis.xsd"
xml:lang="en-US">
<voice gender="female" age="6">Mary had a little lamb,
</voice>
<voice gender="female">
Its fleece was white as snow.
</voice>
<!-- processor-specific voice selection -->
<voice name="Mike">I want to be like Mike.</voice>
<voice gender="female">
Any female voice here.
<p xml:lang="zh-cn">我是中国人。</p>
</voice>
</speak>
其中SynthesizeSsmlToStreamAsync方法是将SSML内容转化为可以播放的音频流。
需要注意的是一般的windows 8.1系统中默认只安装了Microsoft David Desktop - English (United States), Microsoft Hazel Desktop - English (United States),Microsoft Zira Desktop - English (United States) 这三种语音,如果您想朗读上面SSML文件里面的中文,那么您需要另外在您的系统中安装Microsoft Huihui Desktop - Chinese (Simplified) 这个语音。
以下链接可以下载该示例项目的完整代码: