SpeechUtil.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package com.wdkl.callingmainnurse.util;
  2. import android.content.Context;
  3. import android.speech.tts.TextToSpeech;
  4. import android.speech.tts.UtteranceProgressListener;
  5. import android.text.TextUtils;
  6. import com.wdkl.callingmainnurse.common.Constants;
  7. import com.wdkl.callingmainnurse.entity.UdpEntity;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Locale;
  11. public class SpeechUtil {
  12. private static final String TAG = "SpeechUtil";
  13. private TextToSpeech textToSpeech;
  14. private static SpeechUtil speech;
  15. private int speakIndex = 0;
  16. private int loopCount = 2;
  17. private boolean isStop = true;
  18. public volatile static ArrayList<String> speechTextList = new ArrayList<>();
  19. private Thread speechThread;
  20. private boolean isSpeechLoop = true;
  21. private String speakSpeech;
  22. private final Object lockObject = new Object();
  23. public static SpeechUtil getInstance() {
  24. if (speech == null) {
  25. synchronized (SpeechUtil.class) {
  26. if (speech == null) {
  27. speech = new SpeechUtil();
  28. }
  29. }
  30. }
  31. return speech;
  32. }
  33. public void init(Context context) {
  34. textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
  35. @Override
  36. public void onInit(int status) {
  37. if (status == TextToSpeech.SUCCESS) {
  38. int supported = textToSpeech.setLanguage(Locale.CHINESE);
  39. if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
  40. Constants.TTS_STATUS = 1;
  41. LogUtil.d(TAG, "onInit: 当前不支持中文");
  42. } else {
  43. Constants.TTS_STATUS = 2;
  44. LogUtil.d(TAG, "onInit: 支持中文");
  45. }
  46. LogUtil.d(TAG, "onInit: TTS引擎初始化成功");
  47. } else {
  48. Constants.TTS_STATUS = 0;
  49. LogUtil.d(TAG, "onInit: TTS引擎初始化失败");
  50. }
  51. }
  52. }, "com.iflytek.speechcloud");
  53. textToSpeech.setSpeechRate(0.5f);
  54. }
  55. public void newSpeech(String text, boolean emergency) {
  56. synchronized (lockObject) {
  57. if (Constants.DEBUG) {
  58. LogUtil.d(TAG, "start add text speech: " + text);
  59. }
  60. if (speechTextList.contains(text)) {
  61. return;
  62. }
  63. if (Constants.DEBUG) {
  64. LogUtil.d(TAG, "truely add text speech: " + text);
  65. }
  66. if (emergency) {
  67. speechTextList.add(0, text);
  68. } else {
  69. speechTextList.add(text);
  70. }
  71. startSpeechThread();
  72. }
  73. }
  74. public synchronized void speak(final String text) {
  75. if (Constants.DEBUG) {
  76. LogUtil.d(TAG, "tts speak: " + text);
  77. }
  78. isStop = false;
  79. textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, "uniqueId");
  80. textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
  81. @Override
  82. public void onStart(String utteranceId) {
  83. //LogUtil.d(TAG, "speak onStart..." + utteranceId);
  84. }
  85. @Override
  86. public void onDone(String utteranceId) {
  87. speakIndex++;
  88. //LogUtil.d(TAG, "speak onDone...index: " + speakIndex + ", loop: " + loopCount);
  89. if (speakIndex < loopCount) {
  90. //循环播报
  91. speak(text);
  92. } else {
  93. //语音播报完毕
  94. speakIndex = 0;
  95. isStop = true;
  96. }
  97. }
  98. @Override
  99. public void onError(String utteranceId) {
  100. isStop = true;
  101. LogUtil.d(TAG, "speak onError..." + utteranceId);
  102. }
  103. });
  104. }
  105. public void stopSpeak() {
  106. if (textToSpeech.isSpeaking()) {
  107. textToSpeech.stop();
  108. speechTextList.clear();
  109. isStop = true;
  110. speakIndex = 0;
  111. if (Constants.DEBUG) {
  112. LogUtil.d(TAG, "stop speak");
  113. }
  114. }
  115. }
  116. public void removeSpeak(String text) {
  117. synchronized (lockObject) {
  118. if (!TextUtils.isEmpty(text) && !TextUtils.isEmpty(speakSpeech)) {
  119. if (Constants.DEBUG) {
  120. LogUtil.d(TAG, "remove speak: " + text);
  121. }
  122. if (text.equals(speakSpeech) && textToSpeech.isSpeaking()) {
  123. textToSpeech.stop();
  124. speechTextList.remove(text);
  125. isStop = true;
  126. speakIndex = 0;
  127. } else {
  128. speechTextList.remove(text);
  129. }
  130. }
  131. }
  132. }
  133. public void setSpeechLoopCount(int count) {
  134. loopCount = count;
  135. }
  136. public void release() {
  137. speechTextList.clear();
  138. isStop = true;
  139. speakIndex = 0;
  140. if (textToSpeech != null) {
  141. textToSpeech.stop();
  142. textToSpeech.shutdown();
  143. textToSpeech = null;
  144. }
  145. }
  146. public void startSpeechThread() {
  147. if (null == speechThread) {
  148. speechThread = new Thread(new SpeechRunnable());
  149. speechThread.start();
  150. } else if (!speechThread.isAlive()) {
  151. speechThread.start();
  152. }
  153. }
  154. public class SpeechRunnable implements Runnable {
  155. public void run() {
  156. while (isSpeechLoop) {
  157. //synchronized (lockObject) {
  158. if (speechTextList.size() > 0 && isStop) {
  159. speakSpeech = speechTextList.get(0);
  160. if (Constants.DEBUG) {
  161. LogUtil.d(TAG, "speakSpeech: " + speakSpeech);
  162. }
  163. speak(speakSpeech);
  164. //if (speechTextList.contains(speakSpeech)) {
  165. speechTextList.remove(speakSpeech);
  166. //}
  167. }
  168. //}
  169. try {
  170. Thread.sleep(50);
  171. } catch (Exception e) {
  172. e.printStackTrace();
  173. }
  174. }
  175. }
  176. }
  177. }