1、使用DotNetSpeech.dll。
1 1、使用DotNetSpeech.dll。 2 3 /// <summary> 4 5 /// 朗读 6 /// </summary> 7 /// <param name="text">要朗读的文本</param> 8 private void Read(string text) 9 { 10 SpVoice sv = new SpVoice(); 11 sv.Rate = 0;//设置朗读速度 12 SpeechVoiceSpeakFlags SSF = SpeechVoiceSpeakFlags.SVSFlagsAsync; 13 sv.Speak(text, SSF); 14 } 15 /// <summary> 16 /// 生成声音文件 17 /// </summary> 18 /// <param name="text">要朗读的文本</param> 19 /// <param name="filePath">生成声音文件的路径</param> 20 /// <param name="fileName">生成声音文件的名称</param> 21 private void CreateFile(string text, string filePath,string fileName) 22 { 23 if (!Directory.Exists(filePath)) 24 Directory.CreateDirectory(filePath); 25 SpVoice sv = new SpVoice(); 26 SpeechVoiceSpeakFlags SVSF = SpeechVoiceSpeakFlags.SVSFlagsAsync; 27 SpeechStreamFileMode SSFM = SpeechStreamFileMode.SSFMCreateForWrite; 28 SpFileStream SFS = new SpFileStream(); 29 SFS.Open(filePath+fileName, SSFM, false); 30 sv.AudioOutputStream = SFS; 31 sv.Speak(text, SVSF); 32 sv.WaitUntilDone(System.Threading.Timeout.Infinite); 33 SFS.Close(); 34 } 35 2、 使用System.Speech 36 37 SpeechSynthesizer ss = new SpeechSynthesizer(); 38 //播放 39 if (ss != null) 40 { 41 ss.Dispose(); 42 ss.SpeakAsync("朗读的文本"); 43 } 44 //暂停 45 if (ss.State == SynthesizerState.Speaking) 46 { 47 ss.Pause(); 48 } 49 //继续 50 if (reader.State == SynthesizerState.Paused) 51 { 52 ss.Resume(); 53 } 54 //停止 55 if (ss != null) 56 { 57 ss.Dispose(); 58 }
收集整理,非原创。转载:https://www.cnblogs.com/sydeveloper/archive/2013/05/29/3107090.html