bool Voice(string info)
{
int rate = Convert.ToInt16(ReadSetting("SoundRate"));bool Result = false;try{//.net4.0以上版本支持下面的方式// dynamic spVoice = Activator.CreateInstance(Type.GetTypeFromProgID("SAPI.SpVoice", true));// spVoice.Volume = 60; //定义音量大小:0~100 // spVoice.Speak(info);//阻塞进程//// return true;////////////////////////SRead sP = new SRead();Result = sP.SpeakChina(info, VoiceName, 100, rate, saveWavFile);mdg_Decode.write_log("采用SpeechSynthesizer合成语音 ", 1, 1);}catch (Exception e){mdg_Decode.write_log("发生错误:" + e.Message, 1, 1);}finally{if (!Result){mdg_Decode.write_log("采用net4.0以上版本支持的 dynamic spVoice ", 1, 1);//.net4.0以上版本支持下面的方式try{dynamic spVoice = Activator.CreateInstance(Type.GetTypeFromProgID("SAPI.SpVoice"));// spVoice.Voice(VoiceName);// synth.SpeakAsync(sndtext); //异步读//String speechPeople = synth.Voice;//使用 synth 设置朗读音量 [范围 0 ~ 100]spVoice.Volume = 100;//使用 synth 设置朗读频率 [范围 -10 ~ 10]spVoice.Rate = rate;//使用synth 合成 wav 音频文件://synth.SetOutputToWaveFile(string path);spVoice.Speak(info);// spVoice.Dispose();}catch (Exception e){mdg_Decode.write_log("发生错误:" + e.Message, 1, 1);}finally{mdg_Decode.write_log("ok:播放完毕", 1, 1);}}};return true;}public class SRead
{public SpeechSynthesizer synth; //语音合成对象public SRead(){synth = new SpeechSynthesizer();}public SRead(int m, int n){//使用 synth 设置朗读音量 [范围 0 ~ 100]synth.Volume = m;//使用 synth 设置朗读频率 [范围 -10 ~ 10]synth.Rate = n;}public bool SpeakChina(string sndtext, string VoiceName, int Volume=90, int Rate=0,string WavFile=""){bool Result = false;FileStream fs1=null;StreamWriter m_streamWriter1 = null;try{int count = 0;string log_filename = @"Voice_list" + ".txt";fs1 = new FileStream(log_filename, FileMode.Create, FileAccess.Write);m_streamWriter1 = new StreamWriter(fs1);m_streamWriter1.BaseStream.Seek(0, SeekOrigin.End);foreach (InstalledVoice voices in synth.GetInstalledVoices()) //可查看本地电脑已经安装的语音包{count++;m_streamWriter1.WriteLine("已安装语音库:" + voices.VoiceInfo.Name + "\r\n");}if (count > 0){synth.SelectVoice(VoiceName);}}catch (Exception e){mdg_Decode.write_log("查找支持的语音库发生错误:" + e.Message, 1, 1);}finally{m_streamWriter1.Flush();m_streamWriter1.Close();fs1.Close();}try{// synth.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Child, 2, System.Globalization.CultureInfo.CurrentCulture);//Voice.Speak(sndtext, SpFlags);// synth.SpeakAsyncCancelAll();// mdg_Decode.write_log("SpeakAsyncCancelAll:", 1, 1);// synth.SpeakAsync(sndtext); //异步读// mdg_Decode.write_log("Speak:", 1, 1);//String speechPeople = synth.Voice;//使用 synth 设置朗读音量 [范围 0 ~ 100]synth.Volume = Volume;// mdg_Decode.write_log("Volume:", 1, 1);//使用 synth 设置朗读频率 [范围 -10 ~ 10]synth.Rate = Rate;// mdg_Decode.write_log("Rate:", 1, 1);if (WavFile != ""){synth.SetOutputToWaveFile(WavFile);synth.Speak(sndtext);// synth.SpeakAsync(sndtext); //异步读}//使用synth 合成 wav 音频文件:synth.SetOutputToDefaultAudioDevice();synth.Speak(sndtext);//输出到默认声音设备//synth.SpeakAsync(sndtext); //异步读synth.Dispose();Result = true;}catch (Exception e){mdg_Decode.write_log("播放时发生错误:" + e.Message, 1, 1);Result = false;}return Result;}public void SpeakEnglish(string sndtext){//SpVoice Voice = new SpVoice();synth.SelectVoice("VW Julie");synth.Speak(sndtext); //sndtext为要合成的内容synth.Dispose(); }public int m{get{return synth.Volume;}set{synth.Volume = value;}}
public int n
{
get
{
return synth.Rate;
}
set
{
synth.Rate = value;
}
}
}