1、系统采用微软的SAPI 5开发:下载SAPI 5
SAPI 5 SDK: http://download.microsoft.com/download/speechSDK/SDK/5.1/WXP/EN-US/speechsdk51.exe
语言包: http://download.microsoft.com/download/speechSDK/SDK/5.1/WXP/EN-US/speechsdk51LangPack.exe
2、在项目中添加Com组件中引用
3、系统参数定义
SpeechVoiceSpeakFlags SpFlags ;
SpVoice voice = null;
4、在窗体加载事件中实例化参数,并且加载系统已安装语言类库显示至ComBox列表
SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
voice = new SpVoice();
string str = "";
//遍历系统语音类型
for (int i = 0; i < voice.GetVoices(string.Empty, string.Empty).Count; i++)
{
str = voice.GetVoices(string.Empty, string.Empty).Item(i).Id;
str=str.Substring(59,str.Length-59);
comboBox1.Items.Add(str);
}
5、朗读RichTextBox文本框中的文字
string str = "";
遍历系统语音类库
for (int i = 0; i < voice.GetVoices(string.Empty, string.Empty).Count; i++)
{
str = voice.GetVoices(string.Empty, string.Empty).Item(i).Id;
str = str.Substring(59, str.Length - 59);
//判断是否存在中文语音类型
if (str==comboBox1.Text)
{
//设置当前朗读使用的语音类库
voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(i);
voice.Volume = 100;
//朗读
voice.Speak(richTextBox1.Text, SpFlags);
break;
}
}
6、生成音频文件
System.Windows.Forms.SaveFileDialog SFD = new System.Windows.Forms.SaveFileDialog();
SFD.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
SFD.Title = "Save to a wav file";
SFD.FilterIndex = 2;
SFD.RestoreDirectory = true;
if (SFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
SpeechStreamFileMode SSFM = SpeechStreamFileMode.SSFMCreateForWrite;
SpFileStream SFS = new SpFileStreamClass();
SFS.Open(SFD.FileName, SSFM, false);
string str = "";
遍历系统语音类库
for (int i = 0; i < voice.GetVoices(string.Empty, string.Empty).Count; i++)
{
str = voice.GetVoices(string.Empty, string.Empty).Item(i).Id;
str = str.Substring(59, str.Length - 59);
//判断是否存在中文语音类型
if (str == comboBox1.Text)
{
voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(i);
break;
}
}
//音量
voice.Volume = 100;
voice.AudioOutputStream = SFS;
voice.Speak(this.richTextBox1.Text, SpFlags);
voice.WaitUntilDone(System.Threading.Timeout.Infinite);
SFS.Close();
}
7、设置朗读速率
voice.Rate=0;//值范围-10——10
8、源码下载