• 2018-6-8随笔-combox绑定-语音-删空格


     1.下面介绍三种对comboBox绑定的方式,分别是泛型中IList和Dictionary,还有数据集DataTable ----->>>>>飞机票

    2. 简单的语音播报系统

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Speech;
    using System.Speech.Synthesis;
    
    namespace 语音功能1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            SpeechSynthesizer voice = new SpeechSynthesizer();   //创建语音实例
            private void Form1_Load(object sender, EventArgs e)
            {
              
                voice.Rate = -1; //设置语速,[-10,10]
                voice.Volume = 100; //设置音量,[0,100]
               // voice.SpeakAsync("Hellow Word");  //播放指定的字符串,这是异步朗读
    
                //下面的代码为一些SpeechSynthesizer的属性,看实际情况是否需要使用
                //voice.Dispose();  //释放所有语音资源
              // voice.SpeakAsyncCancelAll();  //取消朗读
                voice.Speak("你好! Word");  //同步朗读
                //voice.Pause();  //暂停朗读
               // voice.Resume(); //继续朗读
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
        
                voice.Speak(textBox1.Text);
              
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                voice.Pause();  //暂停朗读
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                 voice.Resume(); //继续朗读
            }
        }
    }

    需要添加引用:引用--添加引用--.NET--Speech引用  添加即可;

    暂停说明:不会像播放器一样,点击暂停立即暂停,会播放完当前的语音,下次播放则不能正常播放

    继续播放:在暂停后需要点击,这样才会正常的播放

    可以每次播放完,释放语音资源,这样会减少性能消耗。 释放是把当前对象释放了,下次播放要重新实例对象

    切记不能死循环播放,这个不能立即停止,所有这样内存会一直占用的。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Speech.Synthesis;
    
    namespace 语音功能1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            SpeechSynthesizer speech;
            private void button2_Click(object sender, EventArgs e)
            {
                try
                    {
                         speech = new SpeechSynthesizer();
                        //同步朗读文本
                        //speech.Speak(richTextBox1.Text);
                        //异步朗读文本
                        speech.SpeakAsync(textBox1.Text);
                        speech.Volume = 100;     //设置音量
                        speech.Rate =-1;    //设置朗读速度
                        //输出文件
                      //  speech.SetOutputToWaveFile(CheckPathTruth(textBox2.Text.Trim()));//输出语言文件
                    }
                    catch (Exception ex)
                    {  MessageBox.Show(ex.Message);    return;   }
    
                }
    
            private void button3_Click(object sender, EventArgs e)
            {
                if (button3.Text == "暂停")
                {
                    speech.Pause();//暂停
                    button3.Text = "继续";
                }
    
                else if (button3.Text == "继续")
                {
                    speech.Resume();//继续
                    button3.Text = "暂停";
                }
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                //停止朗读
                speech.SpeakAsyncCancelAll();
                //释放资源!
                speech.Dispose();
    
            }
    
            }
        }

    上面是我下午写的,发现:异步朗读文本可以用暂停、继续、停止功能,同步朗读是不能的, 想好好理解的,可以看看异步同步问题;

    上面可以去除vs中的多余空格  非常实用的哈

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Speech.Synthesis;
    using System.IO;
    namespace 语音功能1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            SpeechSynthesizer speech;
            private void Form2_Load(object sender, EventArgs e)
            {
                //textBox2.Text = Environment.CurrentDirectory;
                button4.Enabled = false;
                button3.Enabled = false;
            }
            //开始朗读
            private void button2_Click(object sender, EventArgs e)
            {
                if (richTextBox1.Text.Trim() == "")
                { return; }
               
                    try
                    {
                        speech = new SpeechSynthesizer();
                        //输出文件
                        //speech.SetOutputToWaveFile(CheckPathTruth(textBox2.Text.Trim()));//输出语言文件
                        //同步朗读文本
                        //speech.Speak(richTextBox1.Text);
                        //异步朗读文本
                        speech.SpeakAsync(richTextBox1.Text);
                        speech.Volume = (int)num_yl.Value;     //设置音量
                        speech.Rate = (int)num_speed.Value;    //设置朗读速度
                        button3.Enabled = true; button4.Enabled = true;
                    }
                    catch (Exception ex)
                    { MessageBox.Show(ex.Message); return; }
              
            }
            //暂停  和继续
            private void button3_Click(object sender, EventArgs e)
            {
                if (button3.Text == "暂停")
                {
                    speech.Pause();//暂停
                    button3.Text = "继续";
                }
                else if (button3.Text == "继续")
                {
                    speech.Resume();//继续
                    button3.Text = "暂停";
                }
                button2.Enabled = false;
            }
            //停止
            private void button4_Click(object sender, EventArgs e)
            {
                button2.Enabled = true;
                button3.Enabled = false;
                //停止朗读
                speech.SpeakAsyncCancelAll();
                //释放资源!
                speech.Dispose();
                button4.Enabled = false;
            }
            //读取文件
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog open = new OpenFileDialog();
                open.InitialDirectory = "c:\"; //设置初始目录
                open.Multiselect = false;       //设置是否可以选择多个文件
                open.DefaultExt = ".txt";       //设置默认文件拓展名
                open.Filter = "txt文本|*.txt|所以文件|*.*";
                open.ShowHelp = true;       //是否显示帮助按钮
                //判断是否点击了取消或关闭按钮
                //如果不判断,会出现异常
                if (open.ShowDialog() != DialogResult.Cancel)
                {
                    string str = open.FileName;
                    textBox3.Text = str;
                }
                else
                { return; }
                //获取文件内容,放到   richTextBox1  里
                richTextBox1.Text = GetFileStreamOrReadToEnd(textBox3.Text.Trim());
            }
            //保存按钮
            private void button6_Click(object sender, EventArgs e)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Title = "保存文档";
                saveFileDialog.Filter = "*.wav|*.wav|*.mp3|*.mp3";
                saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                DialogResult saveDialog = saveFileDialog.ShowDialog();
                try
                {
                    if (saveDialog == System.Windows.Forms.DialogResult.OK)
                    {
                        textBox1.Text = saveFileDialog.FileName.ToString();
                        SpeechSynthesizer speechSyn = new SpeechSynthesizer();
                        speechSyn.Volume = (int)num_yl.Value;       //音量
                        speechSyn.Rate = (int)num_speed.Value;     //朗读速度
                        speechSyn.SetOutputToWaveFile(CheckPathTruth(textBox1.Text));
                        speechSyn.Speak(richTextBox1.Text);
                        speechSyn.SetOutputToDefaultAudioDevice();
                        MessageBox.Show("保存录音文件成功,保存路径:" + textBox1.Text + "");
                        speechSyn.Dispose();
                    }
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            /// <summary>
            /// 判断文件路径是否正确
            /// </summary>
            /// <param name="path"></param>
            /// <returns>返回正确的文件路径</returns>
            public string CheckPathTruth(string path)
            {
                if (!path.Contains(".wav"))
                {
                    MessageBox.Show("输出文件有误!");
                    return null;
                }
                else
                {
                    return path;
                }
            }
            /// <summary>
            /// 获取文件内容
            /// </summary>
            /// <param name="filepath"></param>
            /// <returns></returns>
            public string GetFileStreamOrReadToEnd(string filepath)
            {
                FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate);
                StreamReader sr = new StreamReader(fs, Encoding.Default);
                //获取所以文本
                string str = sr.ReadToEnd();
                //要关闭!
                fs.Close(); sr.Close();
                return str;
            }
            private void textBox3_TextChanged(object sender, EventArgs e)
            {
                button2.Enabled = true;
                button6.Enabled = true;
            }
        }
    }
    //System.Diagnostics.Process.Start("Explorer.exe", string.Format(@"/select,{0}", saveFileDialog.FileName));//打开wav目录并选中文件
    View Code

    上面是完整的文字转换为语音软件代码:从播放到保存的,利用的都是系统的speech

  • 相关阅读:
    webserver服务器
    轮子哥的网站地址
    构建javaweb服务的docker镜像和容器化启动服务
    spring-cloud-alibaba-gateway
    markdown-记笔记常用语法
    java-生成公私钥命令
    阿里云oss文件服务器
    mybatis-mapper.xml一个标签支持执行多个sql语句
    docker安装nacos
    spring-cloud-alibaba-nacos
  • 原文地址:https://www.cnblogs.com/liuqifeng/p/9154448.html
Copyright © 2020-2023  润新知