|
@@ -4,6 +4,10 @@ import android.content.Context;
|
|
|
import android.speech.tts.TextToSpeech;
|
|
|
import android.speech.tts.UtteranceProgressListener;
|
|
|
|
|
|
+import com.wdkl.callingmainnurse.common.Constants;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import java.util.Locale;
|
|
|
|
|
|
public class SpeechUtil {
|
|
@@ -13,8 +17,10 @@ public class SpeechUtil {
|
|
|
private static SpeechUtil speech;
|
|
|
private int speakIndex = 0;
|
|
|
private int loopCount = 2;
|
|
|
- private String speechText;
|
|
|
private boolean isStop = true;
|
|
|
+ private static List<String> speechTextList = new ArrayList<>();
|
|
|
+ private Thread speechThread;
|
|
|
+ private boolean isSpeechLoop = true;
|
|
|
|
|
|
public static SpeechUtil getInstance() {
|
|
|
if (speech == null) {
|
|
@@ -34,13 +40,15 @@ public class SpeechUtil {
|
|
|
if (status == TextToSpeech.SUCCESS) {
|
|
|
int supported = textToSpeech.setLanguage(Locale.CHINESE);
|
|
|
if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
|
|
|
- //ToastUtil.showToast("当前不支持中文!");
|
|
|
+ Constants.TTS_STATUS = 1;
|
|
|
LogUtil.d(TAG, "onInit: 当前不支持中文");
|
|
|
} else {
|
|
|
+ Constants.TTS_STATUS = 2;
|
|
|
LogUtil.d(TAG, "onInit: 支持中文");
|
|
|
}
|
|
|
LogUtil.d(TAG, "onInit: TTS引擎初始化成功");
|
|
|
} else {
|
|
|
+ Constants.TTS_STATUS = 0;
|
|
|
LogUtil.d(TAG, "onInit: TTS引擎初始化失败");
|
|
|
}
|
|
|
}
|
|
@@ -49,18 +57,15 @@ public class SpeechUtil {
|
|
|
}
|
|
|
|
|
|
public void newSpeech(String text) {
|
|
|
- LogUtil.d(TAG, "start speech: " + text);
|
|
|
- if (loopCount > 0) {
|
|
|
- stopSpeak();
|
|
|
- speechText = text;
|
|
|
- speakIndex = 0;
|
|
|
- speak();
|
|
|
- isStop = false;
|
|
|
- }
|
|
|
+ //LogUtil.d(TAG, "add text speech: " + text);
|
|
|
+ startSpeechThread();
|
|
|
+ speechTextList.add(text);
|
|
|
}
|
|
|
|
|
|
- public void speak() {
|
|
|
- textToSpeech.speak(speechText, TextToSpeech.QUEUE_FLUSH, null, "uniqueId");
|
|
|
+ public synchronized void speak(final String text) {
|
|
|
+ //LogUtil.d(TAG, "start speak");
|
|
|
+ isStop = false;
|
|
|
+ textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, "uniqueId");
|
|
|
textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
|
|
|
@Override
|
|
|
public void onStart(String utteranceId) {
|
|
@@ -71,8 +76,13 @@ public class SpeechUtil {
|
|
|
public void onDone(String utteranceId) {
|
|
|
speakIndex++;
|
|
|
//LogUtil.d(TAG, "speak onDone...index: " + speakIndex + ", loop: " + loopCount);
|
|
|
- if (speakIndex < loopCount && !isStop) {
|
|
|
- speak();
|
|
|
+ if (speakIndex < loopCount) {
|
|
|
+ //循环播报
|
|
|
+ speak(text);
|
|
|
+ } else {
|
|
|
+ //语音播报完毕
|
|
|
+ speakIndex = 0;
|
|
|
+ isStop = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -85,9 +95,10 @@ public class SpeechUtil {
|
|
|
|
|
|
public void stopSpeak() {
|
|
|
if (textToSpeech.isSpeaking()) {
|
|
|
- isStop = true;
|
|
|
textToSpeech.stop();
|
|
|
- LogUtil.d(TAG, "stop speak");
|
|
|
+ speechTextList.clear();
|
|
|
+ isStop = true;
|
|
|
+ //LogUtil.d(TAG, "stop speak");
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -96,6 +107,8 @@ public class SpeechUtil {
|
|
|
}
|
|
|
|
|
|
public void release() {
|
|
|
+ speechTextList.clear();
|
|
|
+ isStop = true;
|
|
|
if (textToSpeech != null) {
|
|
|
textToSpeech.stop();
|
|
|
textToSpeech.shutdown();
|
|
@@ -103,4 +116,34 @@ public class SpeechUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ 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) {
|
|
|
+ if (speechTextList.size() > 0 && isStop) {
|
|
|
+ String speech = speechTextList.get(0);
|
|
|
+ speak(speech);
|
|
|
+
|
|
|
+ if (speechTextList.contains(speech)) {
|
|
|
+ speechTextList.remove(speech);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ Thread.sleep(50);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|