C#实现语音朗读功能
第一步:新建项目 TTS(从文本到语音(TextToSpeech))

第二步:添加引用 System.Speech


第三步:主界面以及后台代码

using System;
using System.Globalization;
using System.Linq;
using System.Speech.Synthesis;
using System.Windows.Forms;
namespace TTS
{
public partial class Form1 : Form
{
private SpeechSynthesizer speech = new SpeechSynthesizer();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
#region 只能读数字和英文
//string text = textBox1.Text;
//if (text.Trim().Length != 0)
//{
// speech.Rate = 5;//语速
// speech.SelectVoice("Microsoft Lili");//设置播音员(中文)
// //speech.SelectVoice("Microsoft Anna"); //英文
// speech.Volume = 100; //音量
// speech.SpeakAsync(textBox1.Text);//语音阅读方法
//}
#endregion
#region 可以读取中文
string phrase = "123我是好人";
SpeechSynthesizer speech = new SpeechSynthesizer();
CultureInfo keyboardCulture = System.Windows.Forms.InputLanguage.CurrentInputLanguage.Culture;
InstalledVoice neededVoice = speech.GetInstalledVoices(keyboardCulture).FirstOrDefault();
if (neededVoice == null)
{
phrase = "Unsupported Language";
}
else if (!neededVoice.Enabled)
{
phrase = "Voice Disabled";
}
else
{
speech.SelectVoice(neededVoice.VoiceInfo.Name);
}
speech.Speak(phrase);
#endregion
}
}
}
源代码下载:https://download.csdn.net/download/longtenggenssupreme/10452762
注:本代码是在是在windows server 2012 R2 上的vs2017 上的4.5版本的环境。
建议Win10 环境,
如果不是上述环境,可能会出现以下:
1、报错
HResult=0x8007007E
Message=检索 COM 类工厂中 CLSID 为 {D9F6EE60-58C9-458B-88E1-2F908FD7F87C} 的组件失败,原因是出现以下错误: 8007007e 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。
Source=System.Speech
StackTrace:
在 System.Speech.Internal.ObjectTokens.RegistryDataKey..ctor(String fullPath, IntPtr regHandle)
在 System.Speech.Internal.ObjectTokens.RegistryDataKey.Open(String registryPath, Boolean fCreateIfNotExist)
在 System.Speech.Internal.ObjectTokens.SAPICategories.DefaultDeviceOut()
在 System.Speech.Internal.Synthesis.VoiceSynthesis..ctor(WeakReference speechSynthesizer)
在 System.Speech.Synthesis.SpeechSynthesizer.get_VoiceSynthesizer()
在 System.Speech.Synthesis.SpeechSynthesizer.set_Rate(Int32 value)
在 TTS.Form1.button1_Click(Object sender, EventArgs e) 在 C:\Users\Administrator\Desktop\TTS\Form1.cs 中: 第 26 行
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 TTS.Program.Main() 在 C:\Users\Administrator\Desktop\TTS\Program.cs 中: 第 19 行
2、或者是下面错误
C#实现语音朗读功能的更多相关文章
- Android 轻松实现语音朗读
语音朗读,这是一个很好的功能,可以实现一些客户的特殊要求.在Android 实现主意功能只需要几段简单的代码即可完成. 在Android 中使用语音朗读功能 只需要使用此类 TextToSpeech ...
- C#语音朗读文本 — TTS的实现
TTS, Text To Speech的缩写,是使用语音朗读文本的技术.目前,在国内应用较多的是排队叫号系统 Windows 平台的TTS,通常使用的是微软自带的 Speech API. Window ...
- Pyqt SpVoice朗读功能
用Pyqt 做一个读取系统剪贴板内容,然后通过语音合成(TTS)朗读出剪贴板的内容 知识要点 SpVoice SpVoice类是支持语音合成(TTS)的核心类.通过SpVoice对象调用TTS引擎,从 ...
- 小梦windows phone 8.1开发:语音朗读
使用SpeechSynthesizer类可以实现文本朗读功能,位于 Windows.Media.SpeechSynthesis命名空间.有了它我们就可以实现有声小说了,是不是很爽.下面给出一个将文本块 ...
- Android Studio快速集成讯飞SDK实现文字朗读功能
今天,我们来学习一下怎么在Android Studio快速集成讯飞SDK实现文字朗读功能,先看一下效果图: 第一步 :了解TTS语音服务 TTS的全称为Text To Speech,即“从文本到语音” ...
- java文字转语音播报功能的实现方法
java文字转语音播报功能的实现方法 一.pom.xml引入jar包依赖 <!-- https://mvnrepository.com/artifact/com.jacob/jacob 文字转语 ...
- 基于Qt5.5.0的sql数据库、SDK_tts文本语音朗读的CET四六级单词背诵系统软件的编写V1.0
作者:小波 QQ:463431476 请关注我的博客园:http://www.cnblogs.com/xiaobo-Linux/ 我的第二款软件:CET四六级单词背诵软件.基于QT5.5.0.sql数 ...
- 实现百度地图导航Demo的语音播报功能
上文中实现了在本地导入百度地图导航Demo,那么在此基础上如何实现导航的语音播报呢? 一.为该应用申请语音播报(也叫注册) http://developer.baidu.com/map/index.p ...
- AngularJS进阶(十八)在AngularJS应用中集成科大讯飞语音输入功能
在AngularJS应用中集成科大讯飞语音输入功能 注:请点击此处进行充电! 前言 根据项目需求,需要在首页搜索框中添加语音输入功能,考虑到科大讯飞语音业务的强大能力,遂决定使用科大讯飞语音输入第三方 ...
随机推荐
- 【刷题】LOJ 2587 「APIO2018」铁人两项
题目描述 比特镇的路网由 \(m\) 条双向道路连接的 \(n\) 个交叉路口组成. 最近,比特镇获得了一场铁人两项锦标赛的主办权.这场比赛共有两段赛程:选手先完成一段长跑赛程,然后骑自行车完成第二段 ...
- js判断checkbox是否选中
$('.div0 .checkbox1').prop('checked')选中返回 true未选中返回 false $('.div0').prop("checked", true) ...
- Python 通过gevent实现协程
#coding:utf-8-*- '''协程(coroutine)又称微线程.纤程,是一种用户级的轻量级线程.协程有自己的寄存器上下文和栈.携程调度时,将寄存器上下文和栈 保存,在切换回来的时候恢复保 ...
- PHP用户输入安全过滤和注入攻击检测
摘抄自ThinkPHP /** * 获取变量 支持过滤和默认值 * @param array $data 数据源 * @param string|false $name 字段名 * @param mi ...
- 同一台机器安装多个MySQL服务
在同一个服务器上面安装多个MySQL服务,甚至是安装多个不同版本(比如5.7和5.7.5.7和8.0.12),关键在于区分安装目录.数据目录和使用端口等. 可以下面步骤 安装MySQL1 wget h ...
- [学习笔记]Cayley-Hilmiton
Cayley–Hamilton theorem - Wikipedia 其实不是理解很透彻,,,先写上 简而言之: 是一个知道递推式,快速求第n项的方法 k比较小的时候可以用矩阵乘法 k是2000,n ...
- 【洛谷P1122】最大子树和
题目大意:给定一棵 N 个节点的无根树,点有点权,点权有正有负,求这棵树的联通块的最大权值之和是多少. 题解:设 \(dp[i]\) 表示以 i 为根节点的最大子树和,那么只要子树的 dp 值大于0, ...
- AIO 开始不定时的抛异常: java.io.IOException: 指定的网络名不再可用
一天里会抛出几个这样的错误,但发现服务还在正常的运行. java.io.IOException: 指定的网络名不再可用. at sun.nio.ch.Iocp.translateErrorToIOEx ...
- HDU 2255 KM算法 二分图最大权值匹配
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- learning hive学习笔记
http://note.youdao.com/noteshare?id=58f314d67b3a04caac36221a9a046a13