1、首先在VS环境下建立一个窗体
2、然后在网上下载一个DotNetSpeech.dll,并添加引用到项目中。
3、代码如下:
3.1 试听部分
private void btnTestListen_Click(object sender, EventArgs e)
{
try
{
SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice voice = new SpVoice();
voice.Volume = 100;
voice.Rate = Convert.ToInt32(txtSpeed.Text);//朗读语速
voice.Speak(this.txtContent.Text, spFlags);
}
catch (Exception)
{
MessageBox.Show("An Error Occured!", "SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
3.2 生成mp3文件部分
private void btnExportMp3_Click(object sender, EventArgs e)
{
try
{
SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice voice = new SpVoice();
voice.Volume = 100;
voice.Rate = Convert.ToInt32(txtSpeed.Text);//朗读语速
voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
sfd.Title = "Save to a wave file";
sfd.FilterIndex = 2;
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
SpFileStream spFileStream = new SpFileStream();
spFileStream.Open(sfd.FileName, SpFileMode, false);
voice.AudioOutputStream = spFileStream;
voice.Speak(txtContent.Text, spFlags);
voice.WaitUntilDone(Timeout.Infinite);
spFileStream.Close();
}
}
catch (Exception)
{
MessageBox.Show("An Error Occured!", "SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
4、代码无法格式化比较乱,请Copy到VS自行缩进!