|
@@ -0,0 +1,180 @@
|
|
|
+package com.wdkl.ncs.android.component.home.util;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.speech.tts.TextToSpeech;
|
|
|
+import android.speech.tts.UtteranceProgressListener;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.util.Log;
|
|
|
+
|
|
|
+
|
|
|
+import com.wdkl.ncs.android.component.nursehome.common.Constants;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Locale;
|
|
|
+
|
|
|
+public class SpeechUtil {
|
|
|
+ private static final String TAG = "SpeechUtil";
|
|
|
+
|
|
|
+ private TextToSpeech textToSpeech;
|
|
|
+ private static SpeechUtil speech;
|
|
|
+ private int speakIndex = 0;
|
|
|
+ private int loopCount = 1;
|
|
|
+ private boolean isStop = true;
|
|
|
+ public volatile static ArrayList<String> speechTextList = new ArrayList<>();
|
|
|
+ private Thread speechThread;
|
|
|
+ private boolean isSpeechLoop = true;
|
|
|
+ private String speakSpeech;
|
|
|
+ private final Object lockObject = new Object();
|
|
|
+
|
|
|
+ public static SpeechUtil getInstance() {
|
|
|
+ if (speech == null) {
|
|
|
+ synchronized (SpeechUtil.class) {
|
|
|
+ if (speech == null) {
|
|
|
+ speech = new SpeechUtil();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return speech;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void init(Context context) {
|
|
|
+ textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
|
|
|
+ @Override
|
|
|
+ public void onInit(int status) {
|
|
|
+ if (status == TextToSpeech.SUCCESS) {
|
|
|
+ int supported = textToSpeech.setLanguage(Locale.CHINESE);
|
|
|
+ if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
|
|
|
+ Constants.Companion.setTts_state(1);
|
|
|
+ Log.d(TAG, "onInit: 当前不支持中文");
|
|
|
+ } else {
|
|
|
+ Constants.Companion.setTts_state(2);
|
|
|
+ Log.d(TAG, "onInit: 支持中文");
|
|
|
+ }
|
|
|
+ Log.d(TAG, "onInit: TTS引擎初始化成功");
|
|
|
+ } else {
|
|
|
+ Constants.Companion.setTts_state(0);
|
|
|
+ Log.d(TAG, "onInit: TTS引擎初始化失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, "com.iflytek.speechcloud");
|
|
|
+ textToSpeech.setSpeechRate(0.5f);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void newSpeech(String text, boolean emergency) {
|
|
|
+ synchronized (lockObject) {
|
|
|
+ if (Constants.Companion.getTts_state() == 2) {
|
|
|
+ if (emergency) {
|
|
|
+ speechTextList.add(0, text);
|
|
|
+ } else {
|
|
|
+ speechTextList.add(text);
|
|
|
+ }
|
|
|
+ startSpeechThread();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized void speak(final String text) {
|
|
|
+ isStop = false;
|
|
|
+ textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, "uniqueId");
|
|
|
+ textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
|
|
|
+ @Override
|
|
|
+ public void onStart(String utteranceId) {
|
|
|
+ //LogUtil.d(TAG, "speak onStart..." + utteranceId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDone(String utteranceId) {
|
|
|
+ speakIndex++;
|
|
|
+ //LogUtil.d(TAG, "speak onDone...index: " + speakIndex + ", loop: " + loopCount);
|
|
|
+ if (speakIndex < loopCount) {
|
|
|
+ //循环播报
|
|
|
+ speak(text);
|
|
|
+ } else {
|
|
|
+ //语音播报完毕
|
|
|
+ speakIndex = 0;
|
|
|
+ isStop = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(String utteranceId) {
|
|
|
+ isStop = true;
|
|
|
+ Log.d(TAG, "speak onError..." + utteranceId);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public void stopSpeak() {
|
|
|
+ if (textToSpeech.isSpeaking()) {
|
|
|
+ textToSpeech.stop();
|
|
|
+ speechTextList.clear();
|
|
|
+ isStop = true;
|
|
|
+ speakIndex = 0;
|
|
|
+ Log.d(TAG, "stop speak");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void removeSpeak(String text) {
|
|
|
+ synchronized (lockObject) {
|
|
|
+ if (!TextUtils.isEmpty(text) && !TextUtils.isEmpty(speakSpeech)) {
|
|
|
+ if (text.equals(speakSpeech) && textToSpeech.isSpeaking()) {
|
|
|
+ textToSpeech.stop();
|
|
|
+ speechTextList.remove(text);
|
|
|
+ isStop = true;
|
|
|
+ speakIndex = 0;
|
|
|
+ } else {
|
|
|
+ speechTextList.remove(text);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setSpeechLoopCount(int count) {
|
|
|
+ loopCount = count;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void release() {
|
|
|
+ speechTextList.clear();
|
|
|
+ isStop = true;
|
|
|
+ speakIndex = 0;
|
|
|
+ if (textToSpeech != null) {
|
|
|
+ textToSpeech.stop();
|
|
|
+ textToSpeech.shutdown();
|
|
|
+ textToSpeech = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void startSpeechThread() {
|
|
|
+ if (null == speechThread) {
|
|
|
+ speechThread = new Thread(new SpeechRunnable());
|
|
|
+ speechThread.start();
|
|
|
+ } else if (!speechThread.isAlive()) {
|
|
|
+ speechThread.start();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class SpeechRunnable implements Runnable {
|
|
|
+ public void run() {
|
|
|
+ while (isSpeechLoop) {
|
|
|
+ //synchronized (lockObject) {
|
|
|
+ if (speechTextList.size() > 0 && isStop) {
|
|
|
+ speakSpeech = speechTextList.get(0);
|
|
|
+ Log.d(TAG, "speakSpeech: " + speakSpeech);
|
|
|
+ speak(speakSpeech);
|
|
|
+
|
|
|
+ //if (speechTextList.contains(speakSpeech)) {
|
|
|
+ speechTextList.remove(speakSpeech);
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ //}
|
|
|
+
|
|
|
+ try {
|
|
|
+ Thread.sleep(50);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|