C#使用System.Speech制作语音提示功能
转载自:C#语音提示
c#实现语音阅读以及文本转语音文件是基于c#的一个类库(SpeechSynthesizer )实现的,使用该类必须要添加引用using System.Speech.Synthesis;直接是无法添加引用的,先对项目进行添加应用
示例图
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace 文本转语音Demo
{
public partial class Form1 : Form
{
private SpeechSynthesizer speech;
/// <summary>
/// 音量
/// </summary>
private int value=100;
/// <summary>
/// 语速
/// </summary>
private int rate;
public Form1()
{
InitializeComponent();
comboBox1.SelectedIndex = 10;
}
private void button1_Click(object sender, EventArgs e)
{
string text=textBox1.Text;
if (text.Trim().Length == 0) {
MessageBox.Show("不能阅读空内容!","错误提示");
return;
}
if (button1.Text == "语音试听") {
speech = new SpeechSynthesizer();
new Thread(Speak).Start();
button1.Text = "停止试听";
}else if(button1.Text=="停止试听"){
speech.SpeakAsyncCancelAll();//停止阅读
button1.Text = "语音试听";
}
}
private void Speak() {
speech.Rate =rate;
speech.SelectVoice("Microsoft Lili");//设置播音员(中文)
//speech.SelectVoice("Microsoft Anna"); //英文
speech.Volume = value;
speech.SpeakAsync(textBox1.Text);//语音阅读方法
speech.SpeakCompleted += speech_SpeakCompleted;//绑定事件
}
/// <summary>
/// 语音阅读完成触发此事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
button1.Text = "语音试听";
}
/// <summary>
/// 拖动进度条事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trackBar1_Scroll(object sender, EventArgs e)
{
//因为trackBar1的值为(0-10)之间而音量值为(0-100)所以要乘10;
value = trackBar1.Value * 10;
}
private void button2_Click(object sender, EventArgs e)
{
string text = textBox1.Text;
if (text.Trim().Length == 0)
{
MessageBox.Show("空内容无法生成!", "错误提示");
return;
}
this.SaveFile(text);
}
/// <summary>
/// 生成语音文件的方法
/// </summary>
/// <param name="text"></param>
private void SaveFile(string text) {
speech = new SpeechSynthesizer();
var dialog = new SaveFileDialog();
dialog.Filter = "*.wav|*.wav|*.mp3|*.mp3";
dialog.ShowDialog();
string path = dialog.FileName;
if (path.Trim().Length == 0)
{
return;
}
speech.SetOutputToWaveFile(path);
speech.Volume = value;
speech.Rate = rate;
speech.Speak(text);
speech.SetOutputToNull();
MessageBox.Show("生成成功!在" + path + "路径中!", "提示");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
rate = Int32.Parse(comboBox1.Text);
}
private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.ReadlocalFile();
}
/// <summary>
/// 读取本地文本文件的方法
/// </summary>
private void ReadlocalFile() {
var open = new OpenFileDialog();
open.ShowDialog();
//得到文件路径
string path = open.FileName;
if (path.Trim().Length == 0)
{
return;
}
var os = new StreamReader(path, Encoding.UTF8);
string str = os.ReadToEnd();
textBox1.Text = str;
}
private void 清空内容ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
}
}
程序运行效果截图
C#使用System.Speech制作语音提示功能相关推荐
- asp.net引用System.Speech实现语音提示
using System; using System.Speech.Synthesis;namespace testvoice {class Program{static void Main(stri ...
- 为自己的软件添加语音提示功能
公司的项目,要求为软件添加语音提示功能. 主要有两个思路: 1.直接播放语音文件,如*.mp3或*.wav 2.将输入的字符串转为语音文件 上一家公司做过播放语音文件,但将输入的字符串转换为语音文件感 ...
- 智能手表语音提示功能芯片选型?NV340D 8脚语音芯片
近年来,智能可穿戴设备逐渐受到消费者的追捧,其中就包括功能多样.佩戴方便的智能手表.智能手表除了有指示时间的功能之外,一般还具有提醒.导航.校准.监测.交互等其中一种或者多种功能. 智能手表实现语音提 ...
- 奥的斯服务器显示spb,奥的斯ACD4电梯自动复位救援与语音提示功能设置操作
如果奥的斯ACD4电梯在采购配置时需要增加复位救援功能,奥的斯电梯工厂将会增加此功能的部件APRS(AdditionalPositionReferenceSystem)系统. 一.此功能的操作特征是: ...
- html5制作语音交互功能,语音交互设计(1):设计流程
文章是作者根据Alexa官方所译,希望能够给你带来些参考和帮助. 会话体验设计中的思考流程 以下内容均来自Alexa官方,本着通俗易懂的原则,在翻译过程中不免有不严谨.错误之处,请各位看官及时批评指正 ...
- UE5 Oculus Quest2 使用跨平台交互语音聊天Cross-Platform Voice Chat Pro制作语音聊天功能 2
1.配置项目 ①打开Config/DefaultEngine.ini添加 [Voice] bEnabled=true [/Script/Engine.Player] ConfiguredInterne ...
- UE5 Oculus Quest2 使用跨平台交互语音聊天Cross-Platform Voice Chat Pro制作语音聊天功能 1
局域网多人联机 1.WidgetMenu追加UI 2. Server按钮蓝图 3.Client按钮蓝图 备注:目前固定了局域网服务器IP地址.有兴趣的小伙伴可自行拓展. 参考:
- 八楼电梯的c语言程序,本科毕业设计—电梯自动语音提示系统.doc
电梯自动语音提示系统 院 系:信息科学与工程学院 专 业 班:电子科学与技术1102班 姓 名: 学 号: 指导教师: 2015年5月 电梯自动语音提示系统 The Elevator Automati ...
- C++调用MicrosoftSpeechPlatformSDK(SAPI)实现文字转语音提示
更多语音提示资料,阅读:为自己的软件添加语音提示功能 微软提供的语音平台开发包: 全称:Speech Recognition and Text-to-Speech Engines for Micros ...
最新文章
- 2015: [Usaco2010 Feb]Chocolate Giving
- mysql修改用户密码
- 2.1JAVA基础复习——JAVA语言的基础组成注释和常量变量
- 【错误记录】Flutter 构建报错 ( Error: Method not found: ‘CupertinoModalPopupRoute‘. | 下载最新 Flutter SDK 版本 )
- mysql.sock的作用
- Java获取泛型T的类型 T.class
- Django create_user with is_active=False
- 【MySQL 】学习笔记千行总结
- 邹建老大写的经典SQL
- 合并两个链表数据结构c语言,合并两个链表.
- linux6.3 dhcp,51CTO博客-专业IT技术博客创作平台-技术成就梦想
- 【zookeeper】zookeeper伪集群模式
- c语言基础--ASCII码表
- 华为mate40e怎样截长图步骤分享
- powerDesign导出word操作步骤
- 俺是一个IT女白领?
- 简单封装retrofit2+rxjava2
- Java图书管理系统(非正式系统任务导向型,内含完整项目代码),编辑Library类并完成TestDriver,南澳大学计算机大作业。
- 190428多线进程编程
- Excel插入图表失真(数据格式原因)修复笔记