public void Speak(string ssml) { SynthesizeQueue.Add(new SynthesizeTask(this, ssml)); OnQueueChanged(); if (SynthesizeThread == null) { SynthesizeThread = new Thread(() => { Stream speechStream = null; while (true) { int i = 0; while (SynthesizeQueue.Count == 0) { Thread.Sleep(10); i++; if (i == 30 * 1000 / 10) { break; } } if (SynthesizeQueue.Count == 0) { break; } SynthesizeTask synthesizeTask = SynthesizeQueue[0]; speechStream = null; try { speechStream = synthesizeTask.Handler.RequestSpeech(synthesizeTask.Ssml); } catch (Exception ex) { synthesizeTask.Handler.OnFailed(ex); } if (speechStream != null) { synthesizeTask.Handler.OnAudioAvailabled(speechStream); } if (SynthesizeQueue.Count > 0) { SynthesizeQueue.RemoveAt(0); synthesizeTask.Clear(); synthesizeTask.Handler.OnQueueChanged(); } } SynthesizeThread = null; }) { IsBackground = true, Name = "TTSClient.SynthesizeThread" }; SynthesizeThread.Start(); } }
public void ClearQueue() { for (int i = 0; i < SynthesizeQueue.Count; i++) { SynthesizeTask synthesizeTask = SynthesizeQueue[i]; if (synthesizeTask.Handler == this) { SynthesizeQueue.RemoveAt(i); synthesizeTask.Clear(); i--; } } OnQueueChanged(); }