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