C#实现语音朗读功能
引用类库:System.Speech.dll
导入空间:
using System.Speech.Synthesis; //用于生成响应的事件
using System.Speech;
using System.Speech.Recognition;
//1.Async="true"
//2.SpeechSynthesizer speak = new SpeechSynthesizer();
//同步朗读
//3.1 speak.Speak(txt.Text.Trim());
//speak.Dispose(); //释放之前的资源
//异步朗读
//3.2 speak.SpeakAsync(txt.Text.Trim());
//speak.Volume = 100; //设置朗读音量 [范围 0 ~ 100]
//speak.Rate = 0; //设置朗读频率 [范围 -10 ~ 10]
//speak.SelectVoice("Microsoft Lili"); //SpeakChina
//speak.SelectVoice("Microsoft Anna"); //SpeakEnglish
//SelectVoiceByHints(VoiceGender.Male, VoiceAge.Child, 2, System.Globalization.CultureInfo.CurrentCulture);
//speak.SpeakAsyncCancelAll(); //取消朗读
//speak.SetOutputToWaveFile("c:\\message.wav");
//语音合成
//4.PromptBuilder pb = new PromptBuilder();
//speak.SelectVoice("Microsoft Lili");
//pb.ClearContent();
//.Net 4.0 的实现方式:
/*
Type type = Type.GetTypeFromProgID("SAPI.SpVoice");
dynamic spVoice = Activator.CreateInstance(type);
spVoice.Speak("你好,欢迎使用 CSharp 4.0!");
*/
//5.语音完成
/*
speak.SpeakCompleted += new EventHandler(speak_SpeakCompleted);
void speak_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
txt.Text = "完成";
}
*/
//SPEAK 开始
/*var reader = new SpeechSynthesizer();
reader.SpeakAsync(txt.Text); */
//event handler
//reader.SpeakCompleted += new EventHandler(reader_SpeakCompleted);
//或
//sp.SpeakCompleted += (s, arg) => txt.Text = "true";
//PAUSE 暂停
/*if (reader.State == SynthesizerState.Speaking)
{
reader.Pause();
btn_pause.Text = "继续";
}*/
//RESUME 继续
/*if (reader.State == SynthesizerState.Paused)
{
reader.Resume();
btn_pause.Text = "暂停";
}*/
//STOP 停止
//reader.Dispose();