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 = -; //设置语速,[-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绑定-语音-删空格的更多相关文章
- winform Combox绑定数据时不触发SelectIndexChanged事件
		
做了一个仓库选择的联动,选了仓库选其下的货区,选了货区选其下的货架分区.每个combox初始化.绑定数据是都会触发SelectIndexChanged事件,相当头疼. 后来无意中在网上看到了一种方法— ...
 - silverlight中Combox绑定数据以及动态绑定默认选定项的用法
		
在Sliverlight中,经常要用到下拉框Combox,然而Combox的数据绑定却是一件令初学者很头疼的事情.今天就来总结一下下拉框的使用方法: 下面写一个简单的例子吧.先写一个日期的Model, ...
 - ComBox绑定枚举
		
(转自:http://blog.csdn.net/chao88552828/article/details/9903159) /// <summary> /// 参数枚举 /// < ...
 - combox绑定后添加自定义列
		
DataTable dt = shUBll.FindAllByWhere(""); DataRow dr = dt.NewRow(); dr["SUID"] = ...
 - winform ComBox绑定数据
		
初始化数据: List<KeyValuePair<string, string>> list: ComBox1.ValueMember = "Key";Co ...
 - combox绑定数据
		
HSMobile_Function.HSMobile_ProjectIDSelect(ProjectID, out dt_Machine);//取出表数据 comboBox_Ma ...
 - 2018.11.2JavaScript随笔
		
构造函数首字母大写 通过new创建对象 BOM:浏览器对象模型
 - php 随笔  截取字符串  跳出循环  去除空格  修改上传文件大小限制
		
substr(string,start,length) echo substr("Hello world",6); world 跳出循环 for($i=1; $i<5; $i ...
 - itchat个人练习 语音与文本图灵测试例程
		
背景介绍 itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单. 使用不到三十行的代码,你就可以完成一个能够处理所有信息的微信机器人. 官方文档参考https://itchat ...
 
随机推荐
- 使用 declare 语句和strict_types 声明来启用严格模式:
			
使用 declare 语句和strict_types 声明来启用严格模式: Caution: 启用严格模式同时也会影响返回值类型声明. Note: 严格类型适用于在启用严格模式的文件内的函数调用,而不 ...
 - django----基于Form组件实现的增删改和基于ModelForm实现的增删改
			
一.ModelForm的介绍 ModelForm a. class Meta: model, # 对应Model的 fields=None, # 字段 exclude=None, # 排除字段 lab ...
 - noip 2018游记
			
憋了好久的游记... 考虑到写游记是oi界的传统,所以还是应该写一篇的. day0: 上午9:30的火车出发,车上颓三国杀! 中午12:00到了大连,下午2:00才开始试机,还是得先去大连大学,在食堂 ...
 - bzoj 4011
			
看了好多篇题解才看懂的题,我实在太菜了... 首先根据一个我不知道的算法,可以证明在没有加入新的边的时候,原图的所有生成树的方案数就是所有点(除1以外)的度之积 那么在新加入这条边之后,我们仍然可以这 ...
 - 饮冰三年-人工智能-linux-07 硬盘分区、格式化及文件系统的管理
			
先给虚拟机添加一个硬盘 通过fdisk -l sdb,查看磁盘内容 通过fdisk /sdb 来操作分区 创建一个新分区 创建第二个分区 创建第三个分区 创建扩展分区 再次创建分区,其实使用的是扩展分 ...
 - 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)
			
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用.为了保证其高可用,单个服务 ...
 - 如何保证Redis的高可用
			
什么是高可用 全年时间里,99%的时间里都能对外提供服务,就是高可用 主备切换 在master故障时,自动检测,将某个slave切换为master的过程,叫做主备切换.这个过程,实现了Redis主从架 ...
 - 微信公众平台开发教程Java版(六) 事件处理(菜单点击/关注/取消关注)
			
https://blog.csdn.net/tuposky/article/details/40589325
 - [转] 最详尽的 JS 原型与原型链终极详解
			
四. __proto__ JS 在创建对象(不论是普通对象还是函数对象)的时候,都有一个叫做__proto__ 的内置属性,用于指向创建它的构造函数的原型对象. 对象 person1 有一个 __pr ...
 - OpenGL的gl.h出现一堆错误,如重定义什么的
			
问题:生成时提示 gl.h中出现一堆错误,如 error C2144: 语法错误 : "void"的前面应有";" error C2182: "API ...