Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System;
  5. using System.Collections.Generic;
  6.  
  7. public class OutputTTS : MonoBehaviour{
  8.  
  9. private TextToSpeechPlugin textToSpeechPlugin;
  10.  
  11. public static OutputTTS instance = null;
  12.  
  13. private string lang = "US";
  14.  
  15. private Queue<string> wordsToSay = new Queue<string>();
  16.  
  17. [SerializeField] Text ttsText;
  18.  
  19. // Use this for initialization
  20. void Start (){
  21. instance = this;
  22. textToSpeechPlugin = TextToSpeechPlugin.GetInstance();
  23. textToSpeechPlugin.SetDebug(0);
  24. textToSpeechPlugin.Initialize();
  25. textToSpeechPlugin.SetPitch (10.0f);
  26. }
  27.  
  28.  
  29. public void setLang(string name)
  30. {
  31. name = name.ToUpper();
  32.  
  33. Dictionary<string, string> ltoc = new Dictionary<string, string>();
  34. ltoc.Add("EN", "US"); ltoc.Add("ES", "SP");
  35. ltoc.Add("CH", "CN"); ltoc.Add("AR", "SA");
  36.  
  37. lang = ltoc.ContainsKey(name)? ltoc[name]: lang;
  38. ttsText.text = "@@@@1";
  39. Debug.Log("TTS: " + lang);
  40. }
  41.  
  42. public void Update()
  43. {
  44. if (IsSpeaking() || wordsToSay.Count == 0)
  45. return;
  46.  
  47. string text = wordsToSay.Peek();
  48.  
  49. textToSpeechPlugin.SetLocaleByCountry(lang);
  50.  
  51. if (textToSpeechPlugin.isInitialized())
  52. {
  53. textToSpeechPlugin.SpeakOut(text, "test - utteranceId");
  54. wordsToSay.Dequeue ();
  55. }
  56. ttsText.text = "@@@@2";
  57.  
  58. }
  59.  
  60. public void TTS(string text)
  61. {
  62. wordsToSay.Enqueue (text);
  63.  
  64. }
  65.  
  66. private void OnApplicationPause(bool val){
  67. //for text to speech events
  68. if(textToSpeechPlugin!=null){
  69. if(textToSpeechPlugin.isInitialized()){
  70. if(val){
  71. textToSpeechPlugin.UnRegisterBroadcastEvent();
  72. ttsText.text = "@@@@4";
  73.  
  74. }else{
  75. textToSpeechPlugin.RegisterBroadcastEvent();
  76. ttsText.text = "@@@@5";
  77.  
  78. }
  79. }
  80. }
  81. }
  82.  
  83. public bool IsSpeaking(){
  84. return textToSpeechPlugin.IsSpeaking();
  85. ttsText.text = "@@@@6";
  86.  
  87. }
  88.  
  89. private void OnDestroy(){
  90. if (instance == this)
  91. instance = null;
  92. //call this of your not going to used TextToSpeech Service anymore
  93. textToSpeechPlugin.StopAllCoroutines ();
  94. textToSpeechPlugin.ShutDownTextToSpeechService();
  95. ttsText.text = "@@@@7";
  96.  
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement