把文字转成语音这个功能是很常用的,如果不追求语音个性,只需要短短几行代码就可以实现。
代码,项目中引用
using System.Speech.Synthesis;
四行代码实现说话:
using (SpeechSynthesizer voice = new SpeechSynthesizer())
{
voice.Rate = 1;
voice.Volume = 100;
voice.Speak("小y设计");
}
封装一下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
namespace SpeakHello
{
public class SoundHelper
{
///
/// 默认音量[0,100]
///
public static int DefaultVolume = 100;
///
/// 默认音速[-10,10]
///
public static int DefaultRate = 1;
public static void Speak(string msg)
{
Speak(msg, DefaultRate, DefaultVolume);
}
///
/// 播报语音
///
/// 内容
/// 语速[-10,10]
/// 音量[0,100]
public static void Speak(string msg, int rate, int volume)
{
using (SpeechSynthesizer voice = new SpeechSynthesizer())
{
voice.Rate = rate;
voice.Volume = volume;
voice.Speak(msg);
}
}
}
}
拓展一下:
除了默认的安娜语音外,还可以安装其他声音:Microsoft Mary,Microsoft Mike和Sample TTS Voice
speaker.SelectVoice("Microsoft Mike");
System.Speech和Microsoft.Speech是不同的,并且为了避免混淆,应该只选择其中之一。
对于System.Speech
- 转到设置/区域和语言/添加语言
- 从语言设置中,下载语音
例如 Helen 位于en_US软件包中。因此,应通过添加英语(美国)语言来下载其他语音。
对于Microsoft.Speech
- 从下面的链接下载语音
- 添加对 项目中的Microsoft.Speech DLL
可以从下面的链接下载Microsoft语音识别和文本到语音引擎数据文件;
用于Microsoft支持的语言的语音识别和文本到语音引擎 https://www.microsoft.com/en-us/download/details.aspx?id=27224
有关更多信息:
Microsoft语音编程指南
https://docs.microsoft.com/en-us/previous-versions/office/developer/speech-technologies/hh378466(v%3doffice.14)
SpeechSynthesizer.SelectVoice方法
https://docs.microsoft.com/en-us/previous-versions/office/developer/speech-technologies/dd167624(v%3Doffice.14)
System.Speech.Synthesis命名空间
https://docs.microsoft.com/en-us/dotnet/api/system.speech.synthesis?view=netframework-4.7.2
Original: https://www.cnblogs.com/tuyile006/p/15840859.html
Author: 小y
Title: 文字转语音小软件
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/547097/
转载文章受原作者版权保护。转载请注明原作者出处!