Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using System;
- using System.Collections.Generic;
- public class OutputTTS : MonoBehaviour{
- private TextToSpeechPlugin textToSpeechPlugin;
- public static OutputTTS instance = null;
- private string lang = "US";
- private Queue<string> wordsToSay = new Queue<string>();
- [SerializeField] Text ttsText;
- // Use this for initialization
- void Start (){
- instance = this;
- textToSpeechPlugin = TextToSpeechPlugin.GetInstance();
- textToSpeechPlugin.SetDebug(0);
- textToSpeechPlugin.Initialize();
- textToSpeechPlugin.SetPitch (10.0f);
- }
- public void setLang(string name)
- {
- name = name.ToUpper();
- Dictionary<string, string> ltoc = new Dictionary<string, string>();
- ltoc.Add("EN", "US"); ltoc.Add("ES", "SP");
- ltoc.Add("CH", "CN"); ltoc.Add("AR", "SA");
- lang = ltoc.ContainsKey(name)? ltoc[name]: lang;
- ttsText.text = "@@@@1";
- Debug.Log("TTS: " + lang);
- }
- public void Update()
- {
- if (IsSpeaking() || wordsToSay.Count == 0)
- return;
- string text = wordsToSay.Peek();
- textToSpeechPlugin.SetLocaleByCountry(lang);
- if (textToSpeechPlugin.isInitialized())
- {
- textToSpeechPlugin.SpeakOut(text, "test - utteranceId");
- wordsToSay.Dequeue ();
- }
- ttsText.text = "@@@@2";
- }
- public void TTS(string text)
- {
- wordsToSay.Enqueue (text);
- }
- private void OnApplicationPause(bool val){
- //for text to speech events
- if(textToSpeechPlugin!=null){
- if(textToSpeechPlugin.isInitialized()){
- if(val){
- textToSpeechPlugin.UnRegisterBroadcastEvent();
- ttsText.text = "@@@@4";
- }else{
- textToSpeechPlugin.RegisterBroadcastEvent();
- ttsText.text = "@@@@5";
- }
- }
- }
- }
- public bool IsSpeaking(){
- return textToSpeechPlugin.IsSpeaking();
- ttsText.text = "@@@@6";
- }
- private void OnDestroy(){
- if (instance == this)
- instance = null;
- //call this of your not going to used TextToSpeech Service anymore
- textToSpeechPlugin.StopAllCoroutines ();
- textToSpeechPlugin.ShutDownTextToSpeechService();
- ttsText.text = "@@@@7";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement