zoukankan      html  css  js  c++  java
  • C#文本转语音,可导出在本地mp3或者wav文件

    整体效果:

    talk is cheap,show you the code!

    private void Speak()
            {
    
                speech.Rate = rate;
                //speech.SelectVoice("Microsoft Lili");//设置播音员(中文)
                //speech.SelectVoice("Microsoft Anna"); //英文
                speech.Volume = value;
                string txt = "";
                this.Invoke((MethodInvoker) delegate
                {
                    txt = rtxt.Text;
                });
    
                speech.SpeakAsync(txt);//语音阅读方法
                speech.SpeakCompleted += speech_SpeakCompleted;//绑定事件
            }
    
            private void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
            {
                this.Invoke((MethodInvoker) delegate
                    {
                        btnTryListen.Text = "语音试听";
                    });
                
            }
    
    
            private void btnSaveFile_Click(object sender, EventArgs e)
            {
                string text = rtxt.Text;
    
                if (text.Trim().Length == 0)
                {
                    MessageBox.Show("空内容无法生成!", "错误提示");
                    return;
                }
    
                this.SaveFile(text);
            }
    
            /// <summary>
            /// 生成语音文件的方法
            /// </summary>
            /// <param name="text"></param>
            private void SaveFile(string text)
            {
                speech = new SpeechSynthesizer();
                var dialog = new SaveFileDialog();
                dialog.Filter = "*.wav|*.wav|*.mp3|*.mp3";
                dialog.ShowDialog();
    
                string path = dialog.FileName;
                if (path.Trim().Length == 0)
                {
                    return;
                }
                speech.SetOutputToWaveFile(path);
                speech.Volume = value;
                speech.Rate = rate;
                speech.Speak(text);
                speech.SetOutputToNull();
                MessageBox.Show("生成成功!在" + path + "路径中!", "提示");
    
            }
    

      

  • 相关阅读:
    Linux centos 6.4安装
    vm虚拟机安装,配置与使用
    Linux简介
    360兼容视图
    补丁patch 漏洞 bug或glitch
    迫不得已! ! 仅仅针对IE浏览器的样式,尤其是IE8及以下
    JS延时器 定时器 暂停器 中断器
    CSS动态定位
    一个标准的AJAX请求
    ajax的两个重要参数contentType 和dataType
  • 原文地址:https://www.cnblogs.com/y114113/p/14040751.html
Copyright © 2011-2022 走看看