Пример #1
0
        internal void PlayAudioClip(AudioClip audioClip, TTSText ttsText, Action onTssEndedAction)
        {
            if (waitForEndOfAudioClipCoroutine != null)
            {
                ChatterBox.Instance.StopCoroutine(waitForEndOfAudioClipCoroutine);
                Debug.LogWarning("Started an audio clip before previous audio clip ended.");
                waitForEndOfAudioClipCoroutine = null;
            }

            if (TextDisplay != null)
            {
                TextDisplay.SetText(ttsText.DisplayText);
            }

            if (AudioSource != null)
            {
                AudioSource.PlayOneShot(audioClip);
            }
            else
            {
                Debug.LogWarning("Unable to play tts. No AudioSource given.");
            }

            waitForEndOfAudioClipCoroutine = ChatterBox.Instance.StartCoroutine(WaitForEndOfAudioClip(onTssEndedAction));
        }
Пример #2
0
        internal void Prepare(TTSText ttsText, TTS tts, Action <Action <Action> > onPreparedHandler, ECachingMode cachingMode = ECachingMode.CacheInFileSystem)
        {
            if (ChatterBox.Instance == null)
            {
                Debug.LogError("Unable to speak text. ChatterBox not ready.");
                return;
            }

            string cacheName = tts.TextToSpeech.GetCacheFilename(ttsText.SpokenText);

            if (cachingMode == ECachingMode.CacheInUnity)
            {
                string    resourceName = cacheName.Substring(0, cacheName.Length - Path.GetExtension(cacheName).Length);
                AudioClip audioClip    = Resources.Load <AudioClip>(resourceName);
                if (audioClip != null)
                {
                    onPreparedHandler(onTssEndedAction => tts.PlayAudioClip(audioClip, ttsText, onTssEndedAction));
                    return;
                }
            }

            if (cachingMode != ECachingMode.NoCaching)
            {
                string fileSystemName = Path.Combine(FilesystemCachePath, cacheName);
                if (File.Exists(fileSystemName))
                {
                    PrepareAudioClip(
                        cacheName,
                        cachingMode,
                        audioClip => onPreparedHandler(onTssEndedAction => tts.PlayAudioClip(audioClip, ttsText, onTssEndedAction))
                        );
                    return;
                }
            }

            // no cache hit. Needs to be generated.
            tts.TextToSpeech.CreateAudioFileFromText(
                ttsText.SpokenText,
                filename => PrepareAudioClip(
                    filename,
                    cachingMode,
                    audioClip => onPreparedHandler(onTssEndedAction => tts.PlayAudioClip(audioClip, ttsText, onTssEndedAction))
                    ),
                error => Debug.LogError(error)
                );
        }