1、使用DotNetSpeech.dll。
/// <summary>
/// 朗读 /// </summary> /// <param name="text">要朗读的文本</param> private void Read(string text) { SpVoice sv = new SpVoice(); sv.Rate = 0;//设置朗读速度 SpeechVoiceSpeakFlags SSF = SpeechVoiceSpeakFlags.SVSFlagsAsync; sv.Speak(text, SSF); } /// <summary> /// 生成声音文件 /// </summary> /// <param name="text">要朗读的文本</param> /// <param name="filePath">生成声音文件的路径</param> /// <param name="fileName">生成声音文件的名称</param> private void CreateFile(string text, string filePath,string fileName) { if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath); SpVoice sv = new SpVoice(); SpeechVoiceSpeakFlags SVSF = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpeechStreamFileMode SSFM = SpeechStreamFileMode.SSFMCreateForWrite; SpFileStream SFS = new SpFileStream(); SFS.Open(filePath+fileName, SSFM, false); sv.AudioOutputStream = SFS; sv.Speak(text, SVSF); sv.WaitUntilDone(System.Threading.Timeout.Infinite); SFS.Close(); }
2、 使用System.Speech SpeechSynthesizer ss = new SpeechSynthesizer(); //播放 if (ss != null) { ss.Dispose(); ss.SpeakAsync("朗读的文本"); } //暂停 if (ss.State == SynthesizerState.Speaking) { ss.Pause(); } //继续 if (reader.State == SynthesizerState.Paused) { ss.Resume(); } //停止 if (ss != null) { ss.Dispose();
} |