首先,要安装Microsoft Speech SDK,可以从微软网站上下载,我安装的是SAPI 5.1。安装完毕以后,会附带chm格式的开发文档。
创建一个C# Winform程序,拖一个RichTextBox控件,添加两个启停按钮。界面可以自己做,我的界面如下所示:
设置朗读属性的界面如下:
在工程中添加应用:SpeechLib,具体方法是添加引用->COM->Microsoft Speech Object Library
SpVoiceClass是朗读的函数,这个函数可以控制是同步朗读还是异步朗读,我在这里选择了是异步朗读。朗读的声音包括英文、中文、日文等,需要安装中文等语音库。这些也可以在微软的网站上找到。SpVoiceClass还可以控制是哪个声音朗读,朗读的语速、音量等属性。异步朗读还有一个问题就是当朗读完毕的时候控制按钮状态的恢复。另外说明:英文版的操作系统和中文版的操作系统中朗读声音的排序是不一样的,其他文章说0代表中文,请不要武断这么认为,在英文操作系统中,0是代表了英文,为了保险起见,可以读取系统中所有的语音列举到组合框以供选择。下面是主要的代码:
[csharp] view plaincopy
- public partial class MainForm : Office2007Form
- {
- bool bStop; // 是否停止
- bool bPause; // 是否暂停
- private Preference ThePreference = new Preference(); // 配置类
- SpVoiceClass SpeechInstance = new SpVoiceClass(); // 朗读类
- public MainForm()
- {
- this.ThePreference.Init();
- InitializeComponent();
- // 自定义事件-配置修改后立即更新
- this.ThePreference.Invalidate += new Preference.InvalidateHandler(InvalidateSpeechCfg);
- bStop = true; // 初始停止
- bPause = false; // 初始没有暂停
- }
- private void MainForm_Load(object sender, EventArgs e)
- {
- // 配置语音、语速和音量
- this.SpeechInstance.Voice = this.SpeechInstance.GetVoices(string.Empty, string.Empty).Item(this.ThePreference.SpeechConfig.Type);
- this.SpeechInstance.Rate = this.ThePreference.SpeechConfig.Rate;
- this.SpeechInstance.Volume = this.ThePreference.SpeechConfig.Volume;
- // 异步模式下,朗读完毕的事件处理
- this.SpeechInstance.EndStream += new SpeechLib._ISpeechVoiceEvents_EndStreamEventHandler(FuncEndStream);
- }
- private void FuncEndStream(int i, object o)
- {
- bStop = true;
- UpdateIcon_playpause();
- }
- /// <summary>
- /// 开始阅读和暂停的按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void toolStripButton_play_pause_Click(object sender, EventArgs e)
- {
- if (bStop == true)
- {
- if (this.richTextBox.Lines.Length == 0)
- {
- bStop = true;
- }
- else
- {
- ReadTextInControl(this.richTextBox.Text);
- if (bPause == true) // 此处一定要有这个判断,因为从Pause状态直接调用Speak函数,不会朗读,除非Resume
- {
- SpeechInstance.Resume();
- bPause = false;
- }
- bStop = false;
- }
- }
- else
- {
- if (bPause == true)
- {
- bPause = false;
- SpeechInstance.Resume();
- }
- else
- {
- bPause = true;
- SpeechInstance.Pause();
- }
- bStop = true;
- }
- UpdateIcon_playpause();
- }
- private void UpdateIcon_playpause()
- {
- if (bStop == true)
- {
- this.toolStripButton_play_pause.Image = ReadFish.Properties.Resources.Play;
- }
- else
- {
- if (bPause == true)
- {
- this.toolStripButton_play_pause.Image = ReadFish.Properties.Resources.Play;
- }
- else
- {
- this.toolStripButton_play_pause.Image = ReadFish.Properties.Resources.Pause;
- }
- }
- }
- public void ReadTextInControl(string strRead)
- {
- SpeechInstance.Speak(strRead, SpeechVoiceSpeakFlags.SVSFlagsAsync); // 异步朗读
- }
- private void toolStripButton_stop_Click(object sender, EventArgs e)
- {
- if (bStop == false)
- {
- SpeechInstance.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak); // 结束朗读
- bStop = true;
- }
- UpdateIcon_playpause();
- }
- private void InvalidateSpeechCfg(object sender, EventArgs e)
- {
- this.SpeechInstance.Voice = this.SpeechInstance.GetVoices(string.Empty, string.Empty).Item(this.ThePreference.SpeechConfig.Type);
- this.SpeechInstance.Rate = this.ThePreference.SpeechConfig.Rate;
- this.SpeechInstance.Volume = this.ThePreference.SpeechConfig.Volume;
- }
- }
- cb_type.Items.Clear();
- SpeechLib.SpVoice voice = new SpeechLib.SpVoice();
- int i = 0;
- foreach (SpeechLib.ISpeechObjectToken t in voice.GetVoices(string.Empty, string.Empty))
- {
- cb_type.Items.Add(t.GetDescription(i++));
- }