使用微软语音库

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

首先是要引入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. Using nxlog4go for Testing Environment

    nxlog4go is very simple to use without any configuring, setting. For example: package main import ( ...

  2. oracle模糊搜索避免使用like,替换为instr()

    oracle中instr()函数用法 instr(name,'张三')>0  相当于  name like '%张三%' instr(name,'张三')=1  相当于  name like ' ...

  3. HDU 6103

    题意: 求最长的两个不相交的子序列,dis <= m : 分析: 当时二分了答案,暴力匹配,TLE了,然后考虑了,O(n^2)预处理出所有区间 dis,然后答案是所有dis中>=m的最长长 ...

  4. 动态规划(DP),最长递增子序列(LIS)

    题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(d ...

  5. 【[JSOI2007]文本生成器】

    \(AC\)机上的计数\(dp\)啊 并没有想到反着求出不合法的串的个数,直接正面硬上 设\(dp[i][j][0/1]\)表示匹配出的长度为\(i\),在\(AC\)机上位置为\(j\),没有/有匹 ...

  6. Netbackup用于技术支持的问题报告(报障模版)

    在与支持部门联系以报告问题之前,请填写以下信息. 日期: _________________________记录以下产品.平台和设备信息:■ 产品及其版本级别.■ 服务器硬件类型和操作系统级别.■ 客 ...

  7. UVA515 King

    嘟嘟嘟 题目翻译:有n个数,m个限制条件.每一个限制条件形如:1.x y gt c:表示ax + ax+1 + … +ay > c.2.x y It c:表示ax + ax+1 + …… +ay ...

  8. POJ 3020 Antenna Placement 【最小边覆盖】

    传送门:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total ...

  9. jsp的4个作用域区别( pageScope、requestScope、sessionScope、applicationScope)

    简单描述 page里的变量没法从index.jsp传递到test.jsp.只要页面跳转了,它们就不见了. request里的变量可以跨越forward前后的两页.但是只要刷新页面,它们就重新计算了.  ...

  10. php常见的几种排序以及二分法查找

    <?php 1.插入排序 思想: 每次将一个待排序的数据元素插入到前面已经排好序的数列中,使数列依然有序,知道待排序数据元素全部插入完为止. 示例: [初始关键字] [49] 38 65 97 ...