網上找了好多資料也沒找到多少 支持返回當前朗讀的位置 用到了C#的回調函數 然後為了方便使用 我多此一舉的封裝了下 沒什麼技術含量 大牛直接飛過 [csharp] using System; using System.Collections.Generic; using System.Text; using DotNetSpeech; using System.Threading; using System.IO; namespace SpVoiceDemo { class SpVoiceUtil { SpVoice voice = new DotNetSpeech.SpVoiceClass(); public delegate void CallBack(bool b,int InputWordPosition, int InputWordLength); /// <summary> /// 朗讀文本 /// </summary> /// <param name="str">要朗讀的文本</param> /// <param name="CallBack">回調地址</param> /// <returns>返回bool</returns> public bool Speak(string str, CallBack CallBack) { int n = voice.Speak(str, SpeechVoiceSpeakFlags.SVSFlagsAsync); Thread thread = new Thread(new ParameterizedThreadStart(Call)); thread.IsBackground = true; thread.Start((Object)CallBack); return !(n!=1); } /// <summary> /// 回調函數線程子程序 /// </summary> /// <param name="callBack"></param> private void Call(Object callBack) { int InputWordLength = 0; //局部_朗讀長度 int InputWordPosition = 0; //局部_朗讀位置 CallBack CallBack = (CallBack)callBack; while ((int)voice.Status.RunningState != 1) { if (InputWordPosition != voice.Status.InputWordPosition || InputWordLength != voice.Status.InputWordLength) { InputWordPosition = voice.Status.InputWordPosition; InputWordLength = voice.Status.InputWordLength; //回調 CallBack(false, InputWordPosition, InputWordLength); } } CallBack(true, InputWordPosition, InputWordLength); } /// <summary> /// 獲取語音庫 /// </summary> /// <returns>List<string></returns> public List<string> getDescription() { List<string> list = new List<string>(); DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices(); int count = obj.Count;//獲取語音庫總數 for (int i = 0; i < count; i++) { string desc = obj.Item(i).GetDescription(); //遍歷語音庫 list.Add(desc); } return list; } /// <summary> /// 設置當前使用語音庫 /// </summary> /// <returns>bool</returns> public bool setDescription(string name) { List<string> list = new List<string>(); DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices(); int count = obj.Count;//獲取語音庫總數 bool result = false; for (int i = 0; i < count; i++) { string desc = obj.Item(i).GetDescription(); //遍歷語音庫 if (desc.Equals(name)) { voice.Voice = obj.Item(i); result = true; } } return result; } /// <summary> /// 設置語速 /// </summary> /// <param name="n"></param> public void setRate(int n) { voice.Rate = n; } /// <summary> /// 設置聲音大小 /// </summary> /// <param name="n"></param> public void setVolume(int n) { voice.Volume = n; } /// <summary> /// 暫停 /// </summary> public void Pause() { voice.Pause(); } /// <summary> /// 繼續 /// </summary> public void Resume() { voice.Resume(); } /// <summary> /// 停止 /// </summary> public void Stop() { voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak); } /// <summary> /// 輸出WAV /// </summary> /// <param name="path">保存路徑</param> /// <param name="str">要轉換的文本內容</param> /// <returns></returns> public bool WreiteToWAV(string path,string str,SpeechAudioFormatType SpAudioType) { SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite; SpFileStream SpFileStream = new SpFileStream(); SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpAudioFormat SpAudio = new DotNetSpeech.SpAudioFormat(); SpAudio.Type = SpAudioType; SpFileStream.Format = SpAudio; SpFileStream.Open(path, SpFileMode, false); voice.AudioOutputStream = SpFileStream; voice.Speak(str, SpFlags); voice.WaitUntilDone(Timeout.Infinite); SpFileStream.Close(); return File.Exists(path); } } }