IEnumerator DownLandFromWeb(string text, Params paramss, UnityAction <AudioClip> OnGet) { var path = AudioPath;//Path可能还没有初始化 string audioName = AudioFileName(text, paramss); bool complete = false; string error = null; TTS_SpeakFinished finishEvent = (result, data) => { if (result == text) { complete = true; } }; TTS_SpeakError errorEvent = (err) => { complete = true; error = err; }; TTS.tts_SpeakFinishedEvent += finishEvent; TTS.ttsSpeakErrorEvent += errorEvent; waitSpeekQueue.Enqueue(new KeyValuePair <string, Params>(text, paramss)); if (downLandThread == null || !downLandThread.IsAlive) { downLandThread = new Thread(ThreadDownland); downLandThread.Start(AudioPath); } yield return(new WaitUntil(() => complete || connectError)); if (connectError) { error = "err:语音联网失败"; } if (error != null) { if (onError != null) { onError.Invoke(error); } else { Debug.LogError(error); } } else { RecordToText(audioName); yield return(AudioHelper.LoadFromFile(AudioPath, audioName, OnGet)); } TTS.tts_SpeakFinishedEvent -= finishEvent; TTS.ttsSpeakErrorEvent -= errorEvent; }
public IEnumerator Downland(string[] text, UnityAction <float> onProgressChanged, Params paramss = null) { if (paramss == null) { paramss = defultParams; } List <string> needDownLand = new List <string>(); foreach (var item in text) { if (!audioHead.audioKey.Contains(AudioFileName(item, paramss))) { needDownLand.Add(item); } } float totalCount = text.Length; float currentCount = totalCount - needDownLand.Count; if (currentCount > 0 && onProgressChanged != null) { onProgressChanged(currentCount / totalCount); } if (needDownLand.Count > 0) { TTS_SpeakFinished finishEvent = (result, data) => { currentCount++; }; TTS_SpeakError errorEvent = (err) => { currentCount++; }; TTS.tts_SpeakFinishedEvent += finishEvent; TTS.ttsSpeakErrorEvent += errorEvent; foreach (var item in needDownLand.ToArray()) { waitSpeekQueue.Enqueue(new KeyValuePair <string, Params>(item, paramss)); } if (downLandThread == null || !downLandThread.IsAlive) { downLandThread = new Thread(ThreadDownland); downLandThread.Start(AudioPath); } var countTemp = currentCount; while (currentCount != totalCount && !connectError) { if (countTemp != currentCount) { if (onProgressChanged != null) { onProgressChanged(currentCount / totalCount); } countTemp = currentCount; } yield return(null); } for (int i = 0; i < needDownLand.Count; i++) { RecordToText(AudioFileName(needDownLand[i], paramss)); } TTS.tts_SpeakFinishedEvent -= finishEvent; TTS.ttsSpeakErrorEvent -= errorEvent; } }