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 ...
随机推荐
- laravel 查询
public function recommends(Request $request) { // $sort = $request->query('sort'); $userId = $req ...
- 用HBuilderX 打包 vue 项目 为 App 的步骤
首先打包你的 vue 项目 生成 dist 文件夹,教程请移步 https://www.cnblogs.com/taohuaya/p/10256670.html 看完上面的教程,请确保 你是 将: ...
- Python匿名函数(lambda)
result = lambda [arg1 [, arg2, .....]]:expression result:用于调用lambda表达式 [arg1 [, arg2, -]]:可选参数,用于传递参 ...
- spring cloud 容错之zuul回退和Zuul过滤器
一.容错:Zuul回退 如果微服务下线了,针对每个微服务,都需要回复一个中文提示,而不是报异常 1.新建ConsumerFallbackProvider.java package com.pupeiy ...
- Ajax增删改查-----------删 改
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 小米8如何root
现身说法,实测有效,也踩坑很多. 0. 准备手机.数据线.windows系统的电脑.小米帐号,各一个. 手机需要装上sim卡:电脑需要能上网.最好是有wifi的环境,用来下载安装包. 注意提前备份数据 ...
- Ubuntu强制重启后提示emergency mode
起因 win10+Ubuntu16.04双系统,在ubuntu下训练一个卷积网但是显存拙计卡死了,于是手贱强制按下电源开关重启. 现象 重启后从grub进ubuntu,并不进图形化的登录界面,而是提示 ...
- 函数sigsuspend
sigqueue函数原型: 函数作用:新的发送信号系统调用,主要是针对实时信号提出的支持信号带有参数,与函数sigaction()配合使用 int sigqueue(pid_t pid, int si ...
- 如何让微信里的html应用弹出“点击右上角分享到朋友圈”的图片
一个分享按钮,一个隐藏的图片(这个图片绝对定位在右上角)然后就是点击显示,点击隐藏了... <a href="javascript:;" onclick="docu ...
- mysql binary
mysql在比较字符串的时候是忽略大些写的 比如有用户叫ABC和abc select * from `sys_user` where username = 'abc' 会出来两条记录 select * ...