利用微软Text-To-Speech朗读文本 - iwateru - 博客园


我超爱看小说,由于长时间盯着电脑眼睛疼,所以从网上下载了一款《读书狼》的软件。虽然达不到真人念书的水平,但是为了保护宝贵的眼睛,这也算是不错的了。然后自己想做一个类似的小工具。而且要是可以的话,可以在生产车间的序列码、注册编号等输入的时候加入语音检测,这样可以防止眼睛疲劳导致的错误。
首先,要安装Microsoft Speech SDK,可以从微软网站上下载,我安装的是SAPI 5.1。安装完毕以后,会附带chm格式的开发文档。
创建一个C# Winform程序,拖一个RichTextBox控件,添加两个启停按钮。界面可以自己做,我的界面如下所示:

设置朗读属性的界面如下:


在工程中添加应用:SpeechLib,具体方法是添加引用->COM->Microsoft Speech Object Library 5.0.
SpVoiceClass是朗读的函数,这个函数可以控制是同步朗读还是异步朗读,我在这里选择了是异步朗读。朗读的声音包括英文、中文、日文等,需要安装中文等语音库。这些也可以在微软的网站上找到。SpVoiceClass还可以控制是哪个声音朗读,朗读的语速、音量等属性。异步朗读还有一个问题就是当朗读完毕的时候控制按钮状态的恢复。另外说明:英文版的操作系统和中文版的操作系统中朗读声音的排序是不一样的,其他文章说0代表中文,请不要武断这么认为,在英文操作系统中,0是代表了英文,为了保险起见,可以读取系统中所有的语音列举到组合框以供选择。下面是主要的代码:
View Code 

public partial class MainForm : Office2007Form
{
    bool bStop;    // 是否停止
    bool bPause; // 是否暂停
    private Preference ThePreference = new Preference();    // 配置类
 
     SpeechLib.SpVoice SpeechInstance = new SpeechLib.SpVoice(); // 朗读类
    //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++));
    }





附件列表

 

转载于:https://www.cnblogs.com/nagual/archive/2012/08/15/88b6ff9d655aad2ba07182b128f16393.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值