using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TextToSpeech
{
public partial class Form1 : Form
{
private SpeechSynthesizer speech;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
/*
using (var synth = new SpeechSynthesizer())
{
synth.SelectVoiceByHints(VoiceGender.Male); // 选择男性发音人员
synth.Volume = 50;// 设置音量大小
synth.Rate = 0; // 设置速度变化值
string textToConvert = "你好";
synth.SpeakAsync(textToConvert); // 开始转换
}
*/
speech = new SpeechSynthesizer();
speech.Rate = 0;
//speech.SelectVoice("Microsoft Lili");//设置播音员(中文)
//speech.SelectVoice("Microsoft Anna"); //英文
speech.Volume = 50;
speech.SpeakAsync(textBox1.Text);//语音阅读方法
}
private void button2_Click(object sender, EventArgs e)
{
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 = 50;
speech.Rate = 0;
speech.Speak(textBox1.Text);
speech.SetOutputToNull();
MessageBox.Show("生成成功!在" + path + "路径中!", "提示");
}
}
}
C#文字转语音代码
最新推荐文章于 2024-07-05 11:43:19 发布