使用微软语音库

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

首先是要引入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. django视图函数解析(三)

    1 视图views概述 1 作用: 视图接受web请求并响应web请求 2 本质: 视图就是python中的处理函数 3 响应: 一般是一个网页的HTML内容.一个重定向.错误信息页面.json格式的 ...

  2. adb工具包使用方法

    ADB工具包总共有四个文件,两个exe后缀,两个dll后缀.里面还带有fastboot.exe下载后在PC上安装,如安装到D:\adb_tools-2.0目录,确认目录中带有fastboot.exe文 ...

  3. vue v-for(数组遍历)

    1.js代码 var box=new Vue({ el:'.box', data:{ msg:['hello','ok','dome'], //定义一个数组 msg2:{a:'ok',b:" ...

  4. OC static 和变量

    #include <stdio.h> // 如果在不同源文件出现了同名的内部变量,那么这些变量将互不干扰 static int b; // 用static修饰的全部变量,可以称为内部变量 ...

  5. sql server 数据库还原后sa连接不上原因

    手动创建了一个同名数据库,然后还原以前或者别人的备份,还原虽然成功了,但是在VS中连接不上,原因可能是: 数据库的所有者中没有添加sa,方法即在数据库名上右击,然后选择文件,在所有者中添加上sa,应该 ...

  6. GraphQL 到底怎么用?看看这个例子就知道了

    转载自: https://www.infoq.cn/article/i5JMm54_aWrRZcem1VgH

  7. python:生成器进阶

    1,列表推导式 值 for 循环 2,生成器表达式 g=(i for i in range(10)) print(g) for i in g: print(i) 3,列表推导式与生成器表达式的区别 # ...

  8. Codeforces Round #429

    Table of Contents A. Generous KefaB. GodsendC. Leha and Function A. Generous Kefa One day Kefa found ...

  9. 【luogu P3366 最小生成树】 模板

    这里是kruskal做法 当然prim也可以,至于prim和kruskal的比较: Prim在稠密图中比Kruskal优,Kruskal在稀疏图中比Prim优. #include<bits/st ...

  10. 自定义AngularJS中的services服务

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...