1、依赖搭建
- 添加引用包 System.Speech
- 工程引用 using System.Speech.Recognition;
2、发声
private void button1_Click(object sender, EventArgs e)
{
using (SpeechSynthesizer synthesizer = new SpeechSynthesizer())
{
//synthesizer.Volume = Convert.ToInt32(textBox3.Text); //设置音量
//synthesizer.Rate = Convert.ToInt32(textBox4.Text); //设置朗读速度
synthesizer.GetInstalledVoices().ToList().ForEach(voice => Console.WriteLine(voice.VoiceInfo.Name));
synthesizer.SelectVoice("Microsoft Huihui Desktop");
synthesizer.Speak(textBox1.Text);
}
}
synthesizer.GetInstalledVoices 获取当前安装支持的语音包名称
获取列表
- Microsoft Huihui Desktop
- Microsoft Zira Desktop
3、语音文件存储
private void button2_Click(object sender, EventArgs e)
{
using (SpeechSynthesizer reader = new SpeechSynthesizer())
{
//set some settings
reader.Volume = 100;
reader.Rate = 0; //medium
reader.SetOutputToWaveFile("1.wav");
reader.Speak(textBox1.Text);
}
}
注意:
- 使用using才能在结束后自动保存语音文件