C#文本转语音,可导出在本地mp3或者wav文件
整体效果:

talk is cheap,show you the code!
private void Speak()
{
speech.Rate = rate;
//speech.SelectVoice("Microsoft Lili");//设置播音员(中文)
//speech.SelectVoice("Microsoft Anna"); //英文
speech.Volume = value;
string txt = "";
this.Invoke((MethodInvoker) delegate
{
txt = rtxt.Text;
});
speech.SpeakAsync(txt);//语音阅读方法
speech.SpeakCompleted += speech_SpeakCompleted;//绑定事件
}
private void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
this.Invoke((MethodInvoker) delegate
{
btnTryListen.Text = "语音试听";
});
}
private void btnSaveFile_Click(object sender, EventArgs e)
{
string text = rtxt.Text;
if (text.Trim().Length == 0)
{
MessageBox.Show("空内容无法生成!", "错误提示");
return;
}
this.SaveFile(text);
}
/// <summary>
/// 生成语音文件的方法
/// </summary>
/// <param name="text"></param>
private void SaveFile(string text)
{
speech = new SpeechSynthesizer();
var dialog = new SaveFileDialog();
dialog.Filter = "*.wav|*.wav|*.mp3|*.mp3";
dialog.ShowDialog();
string path = dialog.FileName;
if (path.Trim().Length == 0)
{
return;
}
speech.SetOutputToWaveFile(path);
speech.Volume = value;
speech.Rate = rate;
speech.Speak(text);
speech.SetOutputToNull();
MessageBox.Show("生成成功!在" + path + "路径中!", "提示");
}

浙公网安备 33010602011771号