使用微软语音库

使用微软语音库可以很快速的制作一个小应用,比如一个唐诗的朗诵工具.本示例也是使用微软语音库,制作了一个唐诗宋词朗诵的应用,仅供加深学习印象

首先是要引入System.Speech库

然后using System.Speech.Synthesis;

此后就可以使用SpeechSynthesizer实例对象来朗诵了

主要代码:

            using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Speech.Synthesis;
        namespace StdioTangShi
{
public partial class FrmMain : Form
{
private string song => @"chinese-poetry-master\json\authors.song.json";
private string tang => @"chinese-poetry-master\json\authors.tang.json";
private List<ShiModel> shiModels = new List<ShiModel>();
public FrmMain()
{
InitializeComponent();
} private void btnSong_Click(object sender, EventArgs e)
{
FrmAuthors frmAuthors = new FrmAuthors();
frmAuthors.AuthorFileName = this.song;
if(frmAuthors.ShowDialog(this) == DialogResult.OK)
{ }
} private void btnTang_Click(object sender, EventArgs e)
{
FrmAuthors frmAuthors = new FrmAuthors();
frmAuthors.AuthorFileName = this.tang;
if (frmAuthors.ShowDialog(this) == DialogResult.OK)
{ }
} private void FrmMain_Load(object sender, EventArgs e)
{
Task task = Task.Run(() => {
this.LoadContent();
});
task.Wait(500);
this.SetContent(this.shiModels[0]);
}
private void LoadContent()
{
List<string> lst = new List<string>()
{
"authors.song.json",
"authors.tang.json",
"poet.song.0.json",
"表面结构字.json"
};
string path = @"chinese-poetry-master\json";
foreach (string fileName in Directory.GetFiles(path))
{
if (lst.Contains(Path.GetFileName(fileName)))
continue;
string content = File.ReadAllText(fileName);
JArray jArray = JArray.Parse(content);
foreach(JToken jitem in jArray)
{
ShiModel shiModel = JsonConvert.DeserializeObject<ShiModel>(jitem.ToString());
this.shiModels.Add(shiModel);
}
}
}
private SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
private void btnSpeech_Click(object sender, EventArgs e)
{
ShiModel shiModel = this.btnSpeech.Tag as ShiModel;
string content = $"{shiModel.Author}{Environment.NewLine}{shiModel.Title}{Environment.NewLine}{shiModel.GetContent()}";
this.speechSynthesizer.Speak(content);
}
private int index = 1;
private void btnNext_Click(object sender, EventArgs e)
{
ShiModel shiModel = this.shiModels[index++];
this.SetContent(shiModel);
}
private void SetContent(ShiModel shiModel)
{
Action action = () => {
this.btnSpeech.Tag = shiModel;
this.rtbContent.Text = shiModel.GetContent();
this.txtAuthor.Text = shiModel.Author;
this.txtTitle.Text = shiModel.Title;
};
this.Invoke(action);
}
private void Start()
{
Random random = new Random();
while(this.btnRand.Tag != null)
{
int index = random.Next(0, this.shiModels.Count);
ShiModel shiModel = this.shiModels[index];
this.SetContent(shiModel);
this.btnSpeech_Click(this.btnSpeech, EventArgs.Empty);
System.Threading.Thread.Sleep(3000);
}
} private void btnRand_Click(object sender, EventArgs e)
{
this.btnRand.Enabled = false;
this.btnRand.Tag = this.btnSpeech.Tag;
Task task = Task.Run(() => {
this.Start();
});
}
}
} </pre>
<p>感谢Github上的大牛分享的唐诗宋词数据<a href="https://github.com/chinese-poetry/chinese-poetry">@chinese-poetry</a></p>

System.Speech使用的更多相关文章

  1. C#中的System.Speech命名空间初探

    本程序是口算两位数乘法,随机生成两个两位数,用语音读出来.然后开启语音识别,接受用户输入,知道答案正确关闭语音识别.用户说答案时,可以说“再说一遍”重复题目. 关键是GrammarBuilder和Ch ...

  2. System.Speech.Synthesis 添加暂停、继续功能

    为了方便调用暂停.继续的方法.要将speech的功能写成一个类.直接附上代码: using System; using System.Collections.Generic; using System ...

  3. asp.net引用System.Speech实现语音提示

    using System; using System.Speech.Synthesis; namespace testvoice { class Program { static void Main( ...

  4. C# 使用System.Speech 进行语音播报和识别

    C# 使用System.Speech 进行语音播报和识别 using System.Speech.Synthesis; using System.Speech.Recognition; //语音识别 ...

  5. 【C#】语音识别 - System.Speech

    一个有趣的东西,今后可能用得上. C#语音识别:在命名空间 System.Speech下SpeechSynthesizer可以将文字转换成语音 贴出代码: public partial class F ...

  6. C#的语音识别 using System.Speech.Recognition;

    using System; using System.Collections.Generic; using System.Linq; using System.Speech.Recognition; ...

  7. Speech两种使用方法

    COM组件使用speech: public class Speach { private static Speach _Instance = null ; private SpeechLib.SpVo ...

  8. Speech语音播报

    System.Speech 这个命名空间,报可以阅读文字和播放音频. 环境  W10 VS2017 CMMT 1.添加程序集引用 System.Speech 2.实例化播音类,并且播放一个文本 Spe ...

  9. C# ms speech文字转语音例子

    最近突发奇想 想玩玩  文字转语音的东东   谷歌了一下 发现微软有一个TTS 的SDK   查了查相关资料  发现 还真不错  然后就开始玩玩Microsoft Speech SDK的 DEMO了 ...

随机推荐

  1. C# 调用 c++ 非托管dll时wchar类型参数的乱码处理

    [DllImport("user32.dll", CallingConvention = CallingConvention.WinApi)] public static exte ...

  2. python课程笔记

    python变量原理:以数值为主,数字存储在内存中,分配给不同的变量.与C刚好相反 Python中,有3种内建的数据结构:列表.元组和字典.1.列表     list是处理一组有序项目的数据结构,即你 ...

  3. 数据结构与算法分析java——树1

    1. 基本术语 度(degree):一个节点的子树个数称为该节点的度: 树中结点度的最大值称为该树的度. 层数(level):从根结点开始算,根节点为1 高度(height)/深度(depth):节点 ...

  4. table中设置tr行间距

    CSS border-collapse 属性设置表格的边框是否被合并为一个单一的边框 值 描述 separate 默认值.边框会被分开.不会忽略 border-spacing 和 empty-cell ...

  5. 2018.11.9 Dubbo入门学习

    1.什么是Dubbo dubbo.io 代表是开源的 DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服 ...

  6. git报错error: src refspec refs/heads/master does not match any.

    $ git pusherror: src refspec refs/heads/master does not match any.error: failed to push some refs 出错 ...

  7. 消息中间件JMS(三)

    1. Spring整合JMS 1.1消息生产者 创建工程springJMS_producer,并在pom文件中引入SpringJms .activeMQ以及单元测试相关依赖 <propertie ...

  8. o'Reill的SVG精髓(第二版)学习笔记——第三章

    第三章:坐标系统 3.1视口 文档打算使用的画布区域称作视口.我们可以在<svg>元素上使用width和height属性确定视口的大小.属性的值可以是一个数字,该数字会被当作用户坐标下的像 ...

  9. Kong Api 初体验

    请查看原文: https://www.fangzhipeng.com/nginx/kong/2016/07/11/kong-api-gateway/ Kong是一个可扩展的开源API层(也称为API网 ...

  10. mysql if...else 的使用

    select case when tca.id = '3' then 'vw' else epc_code end as epccode,tfp.product_id, tfp.vender, tfp ...