C#中英文语音朗读及文字转换成音频文件

1、系统采用微软的SAPI 5开发:下载SAPI 5

SAPI 5 SDK: http://download.microsoft.com/download/speechSDK/SDK/5.1/WXP/EN-US/speechsdk51.exe

语言包: http://download.microsoft.com/download/speechSDK/SDK/5.1/WXP/EN-US/speechsdk51LangPack.exe

2、在项目中添加Com组件中引用


3、系统参数定义

SpeechVoiceSpeakFlags SpFlags ;
SpVoice voice = null;

4、在窗体加载事件中实例化参数,并且加载系统已安装语言类库显示至ComBox列表

SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
 voice = new SpVoice();
string str = "";
            //遍历系统语音类型
for (int i = 0; i < voice.GetVoices(string.Empty, string.Empty).Count; i++)
 {
          str = voice.GetVoices(string.Empty, string.Empty).Item(i).Id;
           str=str.Substring(59,str.Length-59);
           comboBox1.Items.Add(str);
 }

5、朗读RichTextBox文本框中的文字

string str = "";
 遍历系统语音类库
 for (int i = 0; i < voice.GetVoices(string.Empty, string.Empty).Count; i++)
 {
        str = voice.GetVoices(string.Empty, string.Empty).Item(i).Id;
        str = str.Substring(59, str.Length - 59);
       //判断是否存在中文语音类型
         if (str==comboBox1.Text)
          {

//设置当前朗读使用的语音类库
                 voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(i);
                 voice.Volume = 100;

//朗读
                  voice.Speak(richTextBox1.Text, SpFlags);
                  break;
             }
 }

6、生成音频文件

System.Windows.Forms.SaveFileDialog SFD = new System.Windows.Forms.SaveFileDialog();
            SFD.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
            SFD.Title = "Save to a wav file";
            SFD.FilterIndex = 2;
            SFD.RestoreDirectory = true;
            if (SFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                SpeechStreamFileMode SSFM = SpeechStreamFileMode.SSFMCreateForWrite;
                SpFileStream SFS = new SpFileStreamClass();
                SFS.Open(SFD.FileName, SSFM, false);
                string str = "";
                遍历系统语音类库
                for (int i = 0; i < voice.GetVoices(string.Empty, string.Empty).Count; i++)
                {
                    str = voice.GetVoices(string.Empty, string.Empty).Item(i).Id;
                    str = str.Substring(59, str.Length - 59);
                    //判断是否存在中文语音类型
                    if (str == comboBox1.Text)
                    {
                        voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(i);
                        break;
                    }


                }

//音量
                voice.Volume = 100;
                voice.AudioOutputStream = SFS;
                voice.Speak(this.richTextBox1.Text, SpFlags);
                voice.WaitUntilDone(System.Threading.Timeout.Infinite);
                SFS.Close();
            }

7、设置朗读速率

voice.Rate=0;//值范围-10——10

8、源码下载

http://download.csdn.net/detail/bobo14164/5806921

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值