C# 如何朗读字符串

先在引用里添加 System.Speech。

C# 如何朗读字符串

添加using语句

using System.Speech.Synthesis;

然后就是设置播放语音的方法

public void WordSpeak(string s)
{
SpeechSynthesizer speech = new SpeechSynthesizer();
speech.Rate = 1;//设置语速
speech.Volume = 100;//设置音量

////获取本机上所安装的所有的Voice的名称
//string voicestring = “”;
//foreach (InstalledVoice iv in speech.GetInstalledVoices())
//{
// voicestring += iv.VoiceInfo.Name + “,”;
//}
//MessageBox.Show(voicestring);

speech.SelectVoice(“Microsoft Huihui Desktop”);//设置播音员(中文)
speech.SpeakAsync(s);//异步播放
}

播音员在不同的电脑上可能会预装不同的播音员,如果播音员字符串错误,会出现如下异常:

System.ArgumentException:“不能设置语音。未安装匹配的语音,或语音被禁用。”

原创文章,转载请注明:转自于122&&113
本文链接地址:C# 如何朗读字符串


标签:C#