Silverlight语音朗读
时间:2011-07-29 02:32来源:dotblogs
作者:eternaltung 点击: 1086次
Native Extensions for Silverlight (NESL)在上週时发佈了新的版本,NESL让Silverlight在浏览器外执行(Out-of-Browser OOB)时更加增强Silverlight许多功能,它目前有Taskbar的API可以与Windows 7的工作列互相搭配,还有像Speech语音功能相关的API等等,而这篇我就简单示範一下用NESL来实作Silverlight的语音朗读功能 首先
Native Extensions for Silverlight (NESL)在上週时发佈了新的版本,NESL让Silverlight在浏览器外执行(Out-of-Browser OOB)时更加增强Silverlight许多功能,它目前有Taskbar的API可以与Windows 7的工作列互相搭配,还有像Speech语音功能相关的API等等,而这篇我就简单示範一下用NESL来实作Silverlight的语音朗读功能
首先到下面这网址去下载NESL,我选的是第二个Source这包含了程式码以及相关NESL的Libraries
http://code.msdn.microsoft.com/nesl/Release/ProjectReleases.aspx?ReleaseId=5368
接着解压缩后在NESLSourceV2_Preview资料夹内路径(SilverlightLibraries\Microsoft.Silverlight.Windows.Speech\Bin\Release)下可以找到Microsoft.Silverlight.Windows.dll及Microsoft.Silverlight.Windows.Speech.dll这两个档案,先分别右键点选这两个档案的内容后,选择解除封锁方便等一下使用

然后在Silverlight专案References中增加Microsoft.Silverlight.Windows和Microsoft.Silverlight.Windows.Speech的引用

而在介面部分则是很简单的有一个输入方块可以输入要发音的字,以及可以控制发音速度的ComboBox和开始发音的按钮,XAML代码如下:
<UserControl x:Class="SilverlightSpeech.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas x:Name="LayoutRoot" Background="White">
<TextBox Canvas.Left="113" Canvas.Top="16" Height="45" Name="SpeechWord" Width="246" />
<Button Canvas.Left="113" Canvas.Top="122" Content="Speech" Height="27" Name="SpeechButton"
Width="72" Click="SpeechButton_Click" />
<TextBlock Canvas.Left="12" Canvas.Top="34" Height="27" Text="輸入要發音的字" Width="93" />
<ComboBox Canvas.Left="113" Canvas.Top="75" Height="30" Name="RatecomboBox" Width="129" >
<ComboBoxItem Content="0" IsSelected="True"/>
<ComboBoxItem Content="5"/>
<ComboBoxItem Content="10"/>
<ComboBoxItem Content="-5"/>
<ComboBoxItem Content="-10"/>
</ComboBox>
<TextBlock Canvas.Left="26" Canvas.Top="82" Height="27" Name="textBlock2" Text="速度" Width="55" />
</Canvas>
</UserControl>
在程式部分先要引用Microsoft.Silverlight.Windows.Speech.Synthesis这个namespace,然后再利用SpeechSynthesizer的Rate属性来控制发音速度,而要发音时唿叫Speak()方法就可以
using System.Windows;
using System.Windows.Controls;
using Microsoft.Silverlight.Windows.Speech.Synthesis;
namespace SilverlightSpeech
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void SpeechButton_Click(object sender, RoutedEventArgs e)
{
SpeechSynthesizer syn = new SpeechSynthesizer();
syn.Rate = int.Parse((RatecomboBox.SelectedItem as ComboBoxItem).Content.ToString());
syn.Speak(SpeechWord.Text);
}
}
}
Silverlight语音朗读
Native Extensions for Silverlight (NESL)在上週时发佈了新的版本,NESL让Silverlight在浏览器外执行(Out-of-Browser OOB)时更加增强Silverlight许多功能,它目前有Taskbar的API可以与Windows 7的工作列互相搭配,还有像Speech语音功能相关的API等等,而这篇我就简单示範一下用NESL来实作Silverlight的语音朗读功能
首先到下面这网址去下载NESL,我选的是第二个Source这包含了程式码以及相关NESL的Libraries
http://code.msdn.microsoft.com/nesl/Release/ProjectReleases.aspx?ReleaseId=5368
接着解压缩后在NESLSourceV2_Preview资料夹内路径(SilverlightLibraries\Microsoft.Silverlight.Windows.Speech\Bin\Release)下可以找到Microsoft.Silverlight.Windows.dll及Microsoft.Silverlight.Windows.Speech.dll这两个档案,先分别右键点选这两个档案的内容后,选择解除封锁方便等一下使用

然后在Silverlight专案References中增加Microsoft.Silverlight.Windows和Microsoft.Silverlight.Windows.Speech的引用

而在介面部分则是很简单的有一个输入方块可以输入要发音的字,以及可以控制发音速度的ComboBox和开始发音的按钮,XAML代码如下:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas x:Name="LayoutRoot" Background="White">
<TextBox Canvas.Left="113" Canvas.Top="16" Height="45" Name="SpeechWord" Width="246" />
<Button Canvas.Left="113" Canvas.Top="122" Content="Speech" Height="27" Name="SpeechButton"
Width="72" Click="SpeechButton_Click" />
<TextBlock Canvas.Left="12" Canvas.Top="34" Height="27" Text="輸入要發音的字" Width="93" />
<ComboBox Canvas.Left="113" Canvas.Top="75" Height="30" Name="RatecomboBox" Width="129" >
<ComboBoxItem Content="0" IsSelected="True"/>
<ComboBoxItem Content="5"/>
<ComboBoxItem Content="10"/>
<ComboBoxItem Content="-5"/>
<ComboBoxItem Content="-10"/>
</ComboBox>
<TextBlock Canvas.Left="26" Canvas.Top="82" Height="27" Name="textBlock2" Text="速度" Width="55" />
</Canvas>
</UserControl>
在程式部分先要引用Microsoft.Silverlight.Windows.Speech.Synthesis这个namespace,然后再利用SpeechSynthesizer的Rate属性来控制发音速度,而要发音时唿叫Speak()方法就可以
using System.Windows.Controls;
using Microsoft.Silverlight.Windows.Speech.Synthesis;
namespace SilverlightSpeech
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void SpeechButton_Click(object sender, RoutedEventArgs e)
{
SpeechSynthesizer syn = new SpeechSynthesizer();
syn.Rate = int.Parse((RatecomboBox.SelectedItem as ComboBoxItem).Content.ToString());
syn.Speak(SpeechWord.Text);
}
}
}
Silverlight语音朗读(2)
範例程式到这就完成了,不过NESL必须在OOB模式而且是elevated trust模式下才能执行,因此要允许此Silverlight应用程式可以在浏览器外执行。右键点选到Silverlight专案选择Properties

然后Enable running application out of the browser选项打勾,再进入下面OOB settings

在Settings对话方块中,勾选Require elevated trust when running outside the browser

接着在执行时,点击滑鼠右键将此Silverlight应用程式安装到电脑上


然后在OOB且为elevated trust模式下,输入发音的字再按发音钮就会发音啰!

在这裡因为我使用的语音是预设的Microsoft Anna,所以并不能发中文音,如果要查看电脑裡有安装的语音可以到控制台裡找语音辨识再选到文字转换语音就可以看到电脑裡安装的语音了

本文来自eternaltung的博客,原文地址:http://www.dotblogs.com.tw/eternaltung/archive/2011/01/26/speechsynthesizer.aspx


2157

被折叠的 条评论
为什么被折叠?



