|
@@ -3,8 +3,10 @@ package com.wdkl.callingmainnurse.util;
|
|
|
import android.content.Context;
|
|
|
import android.speech.tts.TextToSpeech;
|
|
|
import android.speech.tts.UtteranceProgressListener;
|
|
|
+import android.text.TextUtils;
|
|
|
|
|
|
import com.wdkl.callingmainnurse.common.Constants;
|
|
|
+import com.wdkl.callingmainnurse.entity.UdpEntity;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
@@ -18,9 +20,12 @@ public class SpeechUtil {
|
|
|
private int speakIndex = 0;
|
|
|
private int loopCount = 2;
|
|
|
private boolean isStop = true;
|
|
|
- private volatile static List<String> speechTextList = new ArrayList<>();
|
|
|
+ public volatile static ArrayList<String> speechTextList = new ArrayList<>();
|
|
|
private Thread speechThread;
|
|
|
private boolean isSpeechLoop = true;
|
|
|
+ private Context context;
|
|
|
+ private String speakSpeech;
|
|
|
+ private final Object lockObject = new Object();
|
|
|
|
|
|
public static SpeechUtil getInstance() {
|
|
|
if (speech == null) {
|
|
@@ -34,6 +39,7 @@ public class SpeechUtil {
|
|
|
}
|
|
|
|
|
|
public void init(Context context) {
|
|
|
+ this.context = context;
|
|
|
textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
|
|
|
@Override
|
|
|
public void onInit(int status) {
|
|
@@ -57,9 +63,11 @@ public class SpeechUtil {
|
|
|
}
|
|
|
|
|
|
public void newSpeech(String text) {
|
|
|
- LogUtil.d(TAG, "add text speech: " + text);
|
|
|
- speechTextList.add(text);
|
|
|
- startSpeechThread();
|
|
|
+ synchronized (lockObject) {
|
|
|
+ LogUtil.d(TAG, "add text speech: " + text);
|
|
|
+ speechTextList.add(text);
|
|
|
+ startSpeechThread();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public synchronized void speak(final String text) {
|
|
@@ -88,18 +96,37 @@ public class SpeechUtil {
|
|
|
|
|
|
@Override
|
|
|
public void onError(String utteranceId) {
|
|
|
- //LogUtil.d(TAG, "speak onError..." + utteranceId);
|
|
|
+ isStop = true;
|
|
|
+ LogUtil.d(TAG, "speak onError..." + utteranceId);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
public void stopSpeak() {
|
|
|
- if (textToSpeech.isSpeaking()) {
|
|
|
- textToSpeech.stop();
|
|
|
- speechTextList.clear();
|
|
|
- isStop = true;
|
|
|
- speakIndex = 0;
|
|
|
- LogUtil.d(TAG, "stop speak");
|
|
|
+ synchronized (lockObject) {
|
|
|
+ if (textToSpeech.isSpeaking()) {
|
|
|
+ textToSpeech.stop();
|
|
|
+ speechTextList.clear();
|
|
|
+ isStop = true;
|
|
|
+ speakIndex = 0;
|
|
|
+ LogUtil.d(TAG, "stop speak");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void removeSpeak(String text) {
|
|
|
+ synchronized (lockObject) {
|
|
|
+ if (!TextUtils.isEmpty(text) && !TextUtils.isEmpty(speakSpeech)) {
|
|
|
+ LogUtil.d(TAG, "remove speak: " + text);
|
|
|
+ if (text.equals(speakSpeech) && textToSpeech.isSpeaking()) {
|
|
|
+ textToSpeech.stop();
|
|
|
+ speechTextList.remove(text);
|
|
|
+ isStop = true;
|
|
|
+ speakIndex = 0;
|
|
|
+ } else {
|
|
|
+ speechTextList.remove(text);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -108,13 +135,15 @@ public class SpeechUtil {
|
|
|
}
|
|
|
|
|
|
public void release() {
|
|
|
- speechTextList.clear();
|
|
|
- isStop = true;
|
|
|
- speakIndex = 0;
|
|
|
- if (textToSpeech != null) {
|
|
|
- textToSpeech.stop();
|
|
|
- textToSpeech.shutdown();
|
|
|
- textToSpeech = null;
|
|
|
+ synchronized (lockObject) {
|
|
|
+ speechTextList.clear();
|
|
|
+ isStop = true;
|
|
|
+ speakIndex = 0;
|
|
|
+ if (textToSpeech != null) {
|
|
|
+ textToSpeech.stop();
|
|
|
+ textToSpeech.shutdown();
|
|
|
+ textToSpeech = null;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -130,19 +159,21 @@ public class SpeechUtil {
|
|
|
public class SpeechRunnable implements Runnable {
|
|
|
public void run() {
|
|
|
while (isSpeechLoop) {
|
|
|
- if (speechTextList.size() > 0 && isStop) {
|
|
|
- String speech = speechTextList.get(0);
|
|
|
- LogUtil.d(TAG, "speech: " + speech);
|
|
|
- speak(speech);
|
|
|
-
|
|
|
- if (speechTextList.contains(speech)) {
|
|
|
- speechTextList.remove(speech);
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- Thread.sleep(50);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ synchronized (lockObject) {
|
|
|
+ if (speechTextList.size() > 0 && isStop) {
|
|
|
+ speakSpeech = speechTextList.get(0);
|
|
|
+ LogUtil.d(TAG, "speakSpeech: " + speakSpeech);
|
|
|
+ speak(speakSpeech);
|
|
|
+
|
|
|
+ //if (speechTextList.contains(speakSpeech)) {
|
|
|
+ speechTextList.remove(speakSpeech);
|
|
|
+ //}
|
|
|
+
|
|
|
+ try {
|
|
|
+ Thread.sleep(50);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|