Microsoft Speech Platform案例

简介:

上篇博文说了一些Microsoft Speech Platform的知识点,这篇博文用一个例子来实践一下。

例子是实现一段文字的朗读,朗读到那一句文字,文字就变红色。就这么简单。

先看窗体布局。

104250966.png

实现代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Threading.Tasks;
using  System.Windows.Forms;
using  Microsoft.Speech.Synthesis;
using  System.Threading;
namespace  SpeechPro
{
public  partial  class  PlayForm : Form
{
public  PlayForm()
{
InitializeComponent();
}
/// <summary>
/// TTS对象
/// </summary>
SpeechSynthesizer speech;
/// <summary>
/// 分隔文本内容成数组
/// </summary>
/// <param name="content">文本内容</param>
/// <returns>字符数组</returns>
string [] GetArr( string  content)
{
char [] charArr =  new  char [] {  '。' ',' ';'  }; //分隔字符数组
string [] arr = content.Split(charArr, StringSplitOptions.RemoveEmptyEntries); //分隔
int  sumcount = 0; //字符总数变量
for  ( int  i = 0; i < arr.Length; i++)
{
sumcount += arr[i].Length; //累加变量总数
arr[i] += content.Substring(sumcount, 1); //获取分隔标点符号
sumcount += 1; //加标点符号的长度
}
return  arr;
}
string [] arr;
/// <summary>
/// 播放内容
/// </summary>
void  Play()
{
arr = GetArr(Play_TB.Text);
//设计TTS对象,并设置对象
speech =  new  SpeechSynthesizer();
speech.SetOutputToDefaultAudioDevice();
speech.Volume = 100;
speech.Rate = 0;
speech.SpeakStarted += speech_SpeakStarted;
//异步诵读语音
for  ( int  i = 0; i < arr.Length; i++)
{
PromptBuilder pb =  new  PromptBuilder();
pb.AppendText(arr[i], PromptRate.Medium);
Prompt p =  new  Prompt(pb);
speech.SpeakAsync(p);
}
}
int  index = 0; //朗读索引值
int  sum = 0; //朗读总长度
/// <summary>
/// 朗读Prompt开始时事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void  speech_SpeakStarted( object  sender, SpeakStartedEventArgs e)
{
if  (index > 0)
{
//使上一条记录恢复原来的样式
Play_TB.Select(sum - arr[index - 1].Length, arr[index - 1].Length);
Play_TB.SelectionColor = Color.Black;
}
//设置现在朗读记录的样式
Play_TB.Select(sum, arr[index].Length);
Play_TB.SelectionColor = Color.Red;
//增加长度和朗读索引
sum += arr[index].Length;
index++;
this .Text =  "正在朗读第"  + index +  "句" ;
}
/// <summary>
/// 朗读按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private  void  Play_But_Click( object  sender, EventArgs e)
{
Play();
}
}
}

 
















本文转自桂素伟51CTO博客,原文链接: http://blog.51cto.com/axzxs/1247881,如需转载请自行联系原作者



相关文章
|
SQL 数据库
Microsoft SQL Server 2000数据还原
Microsoft SQL Server 2000还原数据操作流程 1、开始 - 所有程序 - Microsoft SQL Server - 企业管理器(如图1-1)。
1098 0
|
SQL 数据库
Microsoft SQL Server 2000数据附加
Microsoft SQL Server 2000附加数据操作流程 1、开始 - 所有程序 - Microsoft SQL Server - 企业管理器(如图1-1)。
1096 0
|
SQL 数据库 Windows
Microsoft SQL Server 2008数据还原
Microsoft SQL Server 2008还原数据操作流程 1、开始 - 所有程序 - Microsoft SQL Server 2008 R2 - SQL Server Management Studio(如图1-1)。
1116 0
|
SQL 数据库 Windows
Microsoft SQL Server 2000数据库备份
Microsoft SQL Server 2000备份数据操作流程 1、开始 - 所有程序 - Microsoft SQL Server - 企业管理器(如图1-1)。
1218 0
|
SQL 监控 数据库
Microsoft SQL Server 2008清理LDF日志
Microsoft SQL Server 2008 LDF数据文件清理方法。 PS:由于该操作涉及数据库中的数据,请在操作前先做好数据备份。 准备:Microsoft SQL Server 2008清理LDF脚本,请点击下载获取。
1672 0
|
SQL 数据库 Windows
Microsoft SQL Server 2008修改混合模式
数据库身份验证SQL Server和Windows设置 1、开始 - 所有程序 - Microsoft SQL Server 2008 R2 - SQL Server Management Studio(如图1-1) 图 1-1 2、进入后会弹出窗口【连接到服务器】,在【身份验证】选择中【Windows身份验证】— 点击【连接】(如图1-2) 图 1-2 3、在数据库服务实例名(如图1-3,WIN-VQ5SKTLJ9EU\sql位置)点击鼠标右键【属性】。
1192 0
|
SQL 数据库 数据安全/隐私保护
Microsoft SQL Server 2008修改sa密码
数据库sa密码忘记处理方案 1、开始 - 所有程序 - Microsoft SQL Server 2008 R2 - 点击SQL Server Management Studio(如图1-1)。
1551 0
|
SQL 数据库 数据安全/隐私保护
Microsoft SQL Server 2000修改sa密码提示错误:2812
报错提示:错误 2812:未能找到存储过程 ‘ sp_password ’。 准备:存储过程修复脚本,请点击下载获取。下载完毕后,请放置C盘根目录并解压。
1091 0
|
SQL 数据库 Windows
Microsoft SQL Server 2000修改混合模式
数据库身份验证SQL Server和Windows设置 1、开始—所有程序—Microsoft SQL Server—企业管理器,进入Microsoft SQL Server 2000数据库中(如图1-1)。
1217 0
|
SQL 数据库 Windows
Microsoft SQL Server 2000收缩MDF数据
Microsoft SQL Server 2000 MDF数据文件清理方法。 PS:由于该操作涉及数据库中的数据,请在操作前先做好数据备份。 准备:Microsoft SQL Server 2000收缩MDF脚本,请点击下载获取。
1124 0