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. Linux基础实操六

    实操一: 临时配置网络(ip,网关,dns)+永久配置 #ifconfig ens33 192.168.145.134/24 #vim /etc/resolv.conf #route add defa ...

  2. laravel 关联查询

  3. python网络爬虫笔记(三)

    一.切片和迭代 1.列表生成式 2.生成器的generate,但是generate保存的是算法,所以可以迭代计算,没有必要,每次调用generate 二.iteration 循环 1.凡是作用于for ...

  4. HTML中body元素的属性

    body元素的属性 属性 描述 text 设定页面文字颜色 bgcolor 设定页面背景颜色 background 设定页面背景图像 bgproperties 设定页面的背景图像为固定状态(不随页面的 ...

  5. 步步为营-86-WSFUpload组件

    文件上传组件,所需js文件和图片在百度网盘对应的文件夹下 <%@ Page Language="C#" AutoEventWireup="true" Co ...

  6. Spring.Net 简单实例-01(IOC)

    1.话不多说看操作.新建"Windows窗体应用程序" 2:通过配置文件创建IOC容器 首先引入安装包 3:定义一个接口(更好的体现封装性,当然也可以直接使用类) 定义一个类,实现 ...

  7. webstorm ps

    2018WebStorm注册码   2018-10-10 2018年08月22日 17:36:58 阳光明媚的味道 阅读数:6325   8月21日 http://webstorm.autoseasy ...

  8. U32592 摘果实

    链接:https://www.luogu.org/problemnew/show/U32592 题解: 60-70分 二分+网络流

  9. BZOJ 4282(慎二的随机数列

    题解: 网上题解还没看 我的方法是用平衡树维护一个单调栈 由于N用了一定是赚的 所以它的作用是让f[i+1]=f[i]+1 这个是可以记录的 就跟noip蚯蚓那题一样 然后插入一个值的时候查询前面的最 ...

  10. HDU 5977 Garden of Eden (树形dp+快速沃尔什变换FWT)

    CGZ大佬提醒我,我要是再不更博客可就连一月一更的频率也没有了... emmm,正好做了一道有点意思的题,就拿出来充数吧=.= 题意 一棵树,有 $ n (n\leq50000) $ 个节点,每个点都 ...