第一步:新建项目  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、报错

发生了 System.IO.FileNotFoundException
  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、或者是下面错误

转载于:https://www.cnblogs.com/1175429393wljblog/p/9122135.html

C#实现语音朗读功能相关推荐

  1. 关于Google翻译和语音朗读功能无法正常使用的说明

    关于Google翻译和语音朗读功能无法正常使用的说明 2022年10月1日,Google突然停止了Google翻译在中国大陆的业务,不再向中国大陆区域提供翻译服务,官方给出的理由是"因为使用 ...

  2. html增加语音朗读功能,给wordpress主题添加上语音播放文章内容文本朗读功能

    摘要:大挖最近在淘wp主题的时候发现一款主题已经支持了语音朗读功能7b2,大挖一直关注的一些产品和运营网站也陆续上线了语音播放... 大挖最近在淘wp主题的时候发现一款主题已经支持了语音朗读功能7b2 ...

  3. 网页实现文字转语音朗读功能

    使用HTML5的Speech Synthesis API能简单实现语音合成,并进行播报,实现代码如下: <!DOCTYPE html> <html lang="en&quo ...

  4. android 朗读读屏幕,android语音朗读功能demo实现

    简单的文本语音朗读功能实现 实现这个功能主要用一个类,是TextToSpeech,废话不都少,直接上代码.总的来说效果一般,短语的停顿节奏不是很好. 界面很简单,一个Button ,一个EditVie ...

  5. win7系统调用tts的语音朗读功能

    windows的tts组件,正版系统或者win10系统是可以直接调用成功的,但win7有的是阉割版,有的不支持,调用的时候各种异常,网上的人各种抄袭,不知道所以然,还爱瞎bb,我就费了好大劲才完全搞清 ...

  6. python3语音朗读功能

    python3有一个开源库可以支持语音朗读,对中文和英文都有很好的支持 运行前需要下载VS 2019把C++环境配置好和pip install pyttsx3 import pyttsx3 engin ...

  7. 非常简单的语音朗读功能

    不知道大家有没有发现,在System.Speech.Synthesis命名空间下有一个SpeechSynthesizer类,它可以指定的文本内容进行语音朗读,而且,这个类用起来也严重简单,简单到什么程 ...

  8. 原来win10有免费的文字语音朗读功能,配音再不愁了

    最新还在熟悉win10的一些功能.说来惭愧,一直在用win7,直到电脑升级后,很多硬件和软件都只支持win10,于是只能随缘了.不过官方默认的很多骚功能,还真是既低调又强大.与多年前的对win10的印 ...

  9. ios集成语音朗读功能

    使用AVSpeechSynthesizer朗读文字 所有的嗓音如下: "[AVSpeechSynthesisVoice 0x978a0b0] Language: th-TH", & ...

  10. js 实现网页内容语音朗读功能

    案列演示地址 /*!* TtsAudioUtils v1.0* author dengmengxin* date 2019-09-5** @example* ```* // 初始化语音合成配置* // ...

最新文章

  1. Microbiome:植物发育和氮肥共同作用下的小麦根系微生物组
  2. 2018五大人工智能趋势,你知道多少?
  3. 位运算与组合搜索(二)
  4. 对于技术,工程师没有几个写博客的
  5. java调mongodb自定义函数,自定义UDF函数,从hive保存到mongodb
  6. linux mysql 集群安装配置_linux下mysql集群的安装
  7. iOS-保存图片到(自定义)相册步骤
  8. 加载elementor时出现问题_在使用聚氨酯发泡机时出现问题的解决方法!
  9. 遗传算法MATLAB工具箱的下载与安装
  10. Glib实例学习(1)单链表
  11. 时间序列模型 (六):平稳时间序列模型 :自回归AR 、移动平均 MA 、ARMA 模型
  12. CAD:AutoCAD快捷键指令大全(非常值得收藏),本人项目案例实践积累总结
  13. 看工业4.0道路上,美国和德国的不同路径
  14. 数独高阶技巧之八——SDC
  15. 主机访问虚拟机Web服务器
  16. JAVA-JDK配置-JDK下载安装以及环境变量配置(win10)
  17. Sencha Themer
  18. Verilog数字系统设计——数字时钟(带暂停和任意位置位)
  19. python-机器学习-波士顿房价回归分析
  20. OpenGL---PBO

热门文章

  1. 不能将值 NULL 插入列 'ID',表 列不允许有 Null 值
  2. Hive架构及相关函数
  3. Unix时代的开创者Ken Thompson (zz.is2120.bg57iv3)
  4. c语言统计大小写字母 空格 和数字个数,C++统计中英文大小写字母、数字、空格及其他字符个数的方法...
  5. C语言模拟实现memcmp函数
  6. Apache POI Excel固定(冻结)单元格
  7. ReportStudio入门教程(八十四) - 冻结行标题
  8. 清理数据 python_使用python的数据清理技术
  9. 【LInux入门手册】目录结构 常用指令与操作 vim与tmux
  10. JAVA学习导图、思维导图