System.Speech使用
使用微软语音库
使用微软语音库可以很快速的制作一个小应用,比如一个唐诗的朗诵工具.本示例也是使用微软语音库,制作了一个唐诗宋词朗诵的应用,仅供加深学习印象
首先是要引入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使用的更多相关文章
- C#中的System.Speech命名空间初探
本程序是口算两位数乘法,随机生成两个两位数,用语音读出来.然后开启语音识别,接受用户输入,知道答案正确关闭语音识别.用户说答案时,可以说“再说一遍”重复题目. 关键是GrammarBuilder和Ch ...
- System.Speech.Synthesis 添加暂停、继续功能
为了方便调用暂停.继续的方法.要将speech的功能写成一个类.直接附上代码: using System; using System.Collections.Generic; using System ...
- asp.net引用System.Speech实现语音提示
using System; using System.Speech.Synthesis; namespace testvoice { class Program { static void Main( ...
- C# 使用System.Speech 进行语音播报和识别
C# 使用System.Speech 进行语音播报和识别 using System.Speech.Synthesis; using System.Speech.Recognition; //语音识别 ...
- 【C#】语音识别 - System.Speech
一个有趣的东西,今后可能用得上. C#语音识别:在命名空间 System.Speech下SpeechSynthesizer可以将文字转换成语音 贴出代码: public partial class F ...
- C#的语音识别 using System.Speech.Recognition;
using System; using System.Collections.Generic; using System.Linq; using System.Speech.Recognition; ...
- Speech两种使用方法
COM组件使用speech: public class Speach { private static Speach _Instance = null ; private SpeechLib.SpVo ...
- Speech语音播报
System.Speech 这个命名空间,报可以阅读文字和播放音频. 环境 W10 VS2017 CMMT 1.添加程序集引用 System.Speech 2.实例化播音类,并且播放一个文本 Spe ...
- C# ms speech文字转语音例子
最近突发奇想 想玩玩 文字转语音的东东 谷歌了一下 发现微软有一个TTS 的SDK 查了查相关资料 发现 还真不错 然后就开始玩玩Microsoft Speech SDK的 DEMO了 ...
随机推荐
- 一个sql server 实施工程师的反思
自14年开始从事数据库实施,至今(2018-02-16)晃眼间已经四个年头过去了,工作上的能力要求多数能自己解决,可这不应该成为我学习路上的终点.加之总觉得自己对sql 的理解有些浮于表面,所以借着春 ...
- Nginx AWS ELB 域名解析后端502问题
转载:http://liyangliang.me/posts/2016/04/nginx-aws-elb-name-resolution/
- WAKE-WIN10-SOFT-MATio
1,matio Matio is an open-source C library for reading and writing binary MATLAB MAT files. This libr ...
- PHP:使用php,循环html中的select标签与Php数据
select标签,我们都知道是下拉列表,这里,我们使用foreach循环,将select中的数据进行输出 例子: 1.数据表:mimi_article,表中有个字段,为1或0,表示着是或否 2.通过p ...
- Apache轻量级性能測试工具--ab
Apache轻量级性能測试工具--ab ab早已不是什么新奇玩意,平时工作中会须要一些性能測试.简单的性能測试全然能够由AB来替代,而不须要动用LR这样重量级的工具. 此文简介一下ab的工具使用与结果 ...
- linux 安装redis和集群
一.安装redis单机 1.安装编译环境 Redis是c语言开发的. 安装redis需要c语言的编译环境.如果没有gcc需要在线安装.yum install gcc-c++ 2.安装步骤: 第一步:r ...
- C# DataSet.Designer.cs
今天在做项目的时候,发现一个很奇葩的问题,VS 中DataSet数据集的问题Dataset数据集更新,在保存后原有的Dataset.Designer.cs不变,又增加一个新的Dataset1.Desi ...
- 【Linux-CentOS】CentOS安装Win双系统后Win启动项丢失及默认启动项修改
转载自:搁浅bky,有部分更正,建议看此文. 1.Windows启动项消失的原因: 在安装Win7.8/10系统+CentOS7双系统后,默认会将mbr(Main Boot Record)改写为g ...
- 纯JS拖动案例
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- vue2.0移除或更改的一些东西
一.vue2.0移除了$index和$key 虽然说现在很多文章说他们的代码是vue2.0版本的,但是有一些仔细一看,发现并不全是2.0版本,有些语法还是1.0的版本,比如这个$index,$key, ...