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 = -; //设置语速,[-10,10]
voice.Volume = ; //设置音量,[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 = ; //设置音量
speech.Rate =-; //设置朗读速度
//输出文件
// 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目录并选中文件

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

2018-6-8随笔-combox绑定-语音-删空格的更多相关文章

  1. winform Combox绑定数据时不触发SelectIndexChanged事件

    做了一个仓库选择的联动,选了仓库选其下的货区,选了货区选其下的货架分区.每个combox初始化.绑定数据是都会触发SelectIndexChanged事件,相当头疼. 后来无意中在网上看到了一种方法— ...

  2. silverlight中Combox绑定数据以及动态绑定默认选定项的用法

    在Sliverlight中,经常要用到下拉框Combox,然而Combox的数据绑定却是一件令初学者很头疼的事情.今天就来总结一下下拉框的使用方法: 下面写一个简单的例子吧.先写一个日期的Model, ...

  3. ComBox绑定枚举

    (转自:http://blog.csdn.net/chao88552828/article/details/9903159) /// <summary> /// 参数枚举 /// < ...

  4. combox绑定后添加自定义列

    DataTable dt = shUBll.FindAllByWhere(""); DataRow dr = dt.NewRow(); dr["SUID"] = ...

  5. winform ComBox绑定数据

    初始化数据: List<KeyValuePair<string, string>> list: ComBox1.ValueMember = "Key";Co ...

  6. combox绑定数据

    HSMobile_Function.HSMobile_ProjectIDSelect(ProjectID, out dt_Machine);//取出表数据            comboBox_Ma ...

  7. 2018.11.2JavaScript随笔

    构造函数首字母大写 通过new创建对象 BOM:浏览器对象模型

  8. php 随笔 截取字符串 跳出循环 去除空格 修改上传文件大小限制

    substr(string,start,length) echo substr("Hello world",6); world 跳出循环 for($i=1; $i<5; $i ...

  9. itchat个人练习 语音与文本图灵测试例程

    背景介绍 itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单. 使用不到三十行的代码,你就可以完成一个能够处理所有信息的微信机器人. 官方文档参考https://itchat ...

随机推荐

  1. HighCharts基本使用

    一.简叙 HighCharts是一个非常强大的画图插件,在以后的工作汇报,数字展示,它将是一把利器.既然是插件,那么就有它的使用规则,我们只需要遵循它的使用规则,就可以画出我们想要的展示效果了.期待吗 ...

  2. Java 并发类

    java.util.concurrent包里 提供了一批线程安全的类 一. java.util.concurrent.atomic java.util.concurrent.atomic包里的原子处理 ...

  3. Client-Side Attacks

    1.之前看到中间人攻击方式,要使用ssl服务构架一个劫持会话,使得攻击者和被攻击者客户端连接.ssl 服务(secure Socket Layer安全套接) ,以及后续出现的TSL(Transport ...

  4. C++ 关闭显示器

    好困,想躺一下,关灯.上床,笔记本的屏幕还亮着,好刺眼,睡不着! 脑子里出现一个疑问,怎么用C++写一个关闭屏幕的小程序呢? 参考了网上已有的例子,最简化: #include <windows. ...

  5. Redis创建集群报错

    Redis创建集群报错: 1:任何一个集群节点中都不能存在数据,如果有备份一下删除掉aof文件或rdb文件 2: nodes-集群端口.conf 文件存的会有报错记录,所以该文件也要删除

  6. 首次使用idea步骤

    修改代码提示快捷键 默认是ctrl+空格,这个会跟中英文切换的快捷键冲突. 我这里改成了alt+/ tomcat的配置

  7. PAT Basic 1065 单身狗

    单身狗(25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue "单身狗"是中文对于单身人士的 ...

  8. Junit4 IDEA测试学习一

    1.Junit4 下载 https://github.com/junit-team/junit4/releases 4.12 还需要还导入hamcrest-core-1.3.jar 2.IDEA Te ...

  9. rabbitmq3.7.5 centos7 安装笔记

    先安装各种依赖文件: yum -y install gcc glibc-devel make ncurses-devel openssl-devel xmlto perl wget vim 1. ra ...

  10. ubuntu安装php+mysql+apche

    步骤一,安装apache2 ? sudo apt-get install apache2 安装完成. 运行如下命令重启下: ? sudo /etc/init.d/apache2 restart 在浏览 ...