使用微软语音库

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

首先是要引入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. jetbrain rider 逐渐完美了,微软要哭了么?

    2019-03-24 10:08:42 多年的vsiual studio使用经验,各种小瑕疵:到现在的visual studio是越来越大了:简直到了无法忍受境地: 每次重装系统都要重新安装下,这个不 ...

  2. 用AutoHotkey调整Windows音量

    我用了[右Alt]+方向键来调整音量:Alt+上下键,音量调整幅度为5,如果再增加个右Ctrl,音量调整幅度为1. Alt+左键为静音,Alt+右键为最大音量. >!Up:: ;音量+ < ...

  3. April 1 2017 Week 13 Saturday

    There is more to life than increasing its speed. 生活不仅仅是匆匆赶路. Get a life, a real life, not a manic pu ...

  4. VS LNK2019 解决办法之一

    LNK2019: unresolved external symbol _main referenced in function __main 有人说这是因为静态动态引用引起的,但是!这些都没有解决我 ...

  5. HDU 1521 指数型母函数

    方法一: DFS 方法二:生成函数 每个数可以重复一定次数,求排列组合数,这是裸的指数型生成函数: #include <bits/stdc++.h> using namespace std ...

  6. 【转】Android 之ActivityThead、ActivityManagerService 与activity的管理和创建

    在android中,Activity是四大组件中比较重要的一个(当然其他的也比较重要),那么android中是怎样管理这些activity的?应用的进程和主线程是怎么创建的,应用的消息循环又是在什么时 ...

  7. 牛客国庆集训派对Day2 F、平衡二叉树 【构造+记忆化搜索】

    任意门:https://www.nowcoder.com/acm/contest/202/F 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 1048576K,其他语言2097152K6 ...

  8. require,import区别?

    遵循的模块化规范不一样 模块化规范:即为 JavaScript 提供一种模块编写.模块依赖和模块运行的方案.谁让最初的 JavaScript 是那么的裸奔呢——全局变量就是它的模块化规范. requi ...

  9. servlet三种方式实现servlet接口

    简单介绍 Servlet接口实现类 1.Servlet接口SUN公司定义了两个默认实现类,分别为:GenericServlet.HttpServlet. 2.HttpServlet指能够处理HTTP请 ...

  10. iOS程序的启动原理

    一.在一些老的项目中,有很多不是通过代码添加窗口和跟控制器.是通过设置MainInterface(设置最主要的xib),在xib中通过图形化的操作设置应用程序的代理->设置窗口->设置根控 ...