一、Nuget包
注意版本号,例如你的是NET6,那就要安装NET6版本的Nuget

二、源代码
cs
using System.Speech.Synthesis;
class Program
{
static void Main()
{
string txt = "你好世界!这里是C#文字转语音";
PlayTxt(txt);
}
private static void PlayTxt(string txt)
{
using (var synth = new SpeechSynthesizer())
{
// 设置中文语音(Windows 内置)
synth.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult, 0, new System.Globalization.CultureInfo("zh-CN"));
synth.Volume = 100; // 0-100
synth.Rate = 0; // -10 到 10
synth.Speak(txt); // 同步朗读,直到结束
}
}
}