0
点赞
收藏
分享

微信扫一扫

文字转语音小软件

他说Python 2022-03-11 阅读 50


把文字转成语音这个功能是很常用的,如果不追求语音个性,只需要短短几行代码就可以实现。

文字转语音小软件_c#

下载

代码,项目中引用

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
{
/// <summary>
/// 默认音量[0,100]
/// </summary>
public static int DefaultVolume = 100;
/// <summary>
/// 默认音速[-10,10]
/// </summary>
public static int DefaultRate = 1;

public static void Speak(string msg)
{
Speak(msg, DefaultRate, DefaultVolume);
}
/// <summary>
/// 播报语音
/// </summary>
/// <param name="msg">内容</param>
/// <param name="rate">语速[-10,10]</param>
/// <param name="volume">音量[0,100]</param>
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


  1. 转到设置/区域和语言/添加语言
  2. 从语言设置中,下载语音

例如 Helen 位于en_US软件包中。因此,应通过添加英语(美国)语言来下载其他语音。

对于Microsoft.Speech


  1. 从下面的链接下载语音
  2. 添加对     项目中的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

-----------------------------------------------------------------






举报

相关推荐

0 条评论