|
@@ -23,7 +23,6 @@ public class SpeechUtil {
|
|
public volatile static ArrayList<String> speechTextList = new ArrayList<>();
|
|
public volatile static ArrayList<String> speechTextList = new ArrayList<>();
|
|
private Thread speechThread;
|
|
private Thread speechThread;
|
|
private boolean isSpeechLoop = true;
|
|
private boolean isSpeechLoop = true;
|
|
- private Context context;
|
|
|
|
private String speakSpeech;
|
|
private String speakSpeech;
|
|
private final Object lockObject = new Object();
|
|
private final Object lockObject = new Object();
|
|
|
|
|
|
@@ -39,7 +38,6 @@ public class SpeechUtil {
|
|
}
|
|
}
|
|
|
|
|
|
public void init(Context context) {
|
|
public void init(Context context) {
|
|
- this.context = context;
|
|
|
|
textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
|
|
textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
|
|
@Override
|
|
@Override
|
|
public void onInit(int status) {
|
|
public void onInit(int status) {
|
|
@@ -64,7 +62,9 @@ public class SpeechUtil {
|
|
|
|
|
|
public void newSpeech(String text, boolean emergency) {
|
|
public void newSpeech(String text, boolean emergency) {
|
|
synchronized (lockObject) {
|
|
synchronized (lockObject) {
|
|
- LogUtil.d(TAG, "add text speech: " + text);
|
|
|
|
|
|
+ if (Constants.DEBUG) {
|
|
|
|
+ LogUtil.d(TAG, "add text speech: " + text);
|
|
|
|
+ }
|
|
if (emergency) {
|
|
if (emergency) {
|
|
speechTextList.add(0, text);
|
|
speechTextList.add(0, text);
|
|
} else {
|
|
} else {
|
|
@@ -75,7 +75,9 @@ public class SpeechUtil {
|
|
}
|
|
}
|
|
|
|
|
|
public synchronized void speak(final String text) {
|
|
public synchronized void speak(final String text) {
|
|
- LogUtil.d(TAG, "start speak");
|
|
|
|
|
|
+ if (Constants.DEBUG) {
|
|
|
|
+ LogUtil.d(TAG, "tts speak: " + text);
|
|
|
|
+ }
|
|
isStop = false;
|
|
isStop = false;
|
|
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, "uniqueId");
|
|
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, "uniqueId");
|
|
textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
|
|
textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
|
|
@@ -107,12 +109,12 @@ public class SpeechUtil {
|
|
}
|
|
}
|
|
|
|
|
|
public void stopSpeak() {
|
|
public void stopSpeak() {
|
|
- synchronized (lockObject) {
|
|
|
|
- if (textToSpeech.isSpeaking()) {
|
|
|
|
- textToSpeech.stop();
|
|
|
|
- speechTextList.clear();
|
|
|
|
- isStop = true;
|
|
|
|
- speakIndex = 0;
|
|
|
|
|
|
+ if (textToSpeech.isSpeaking()) {
|
|
|
|
+ textToSpeech.stop();
|
|
|
|
+ speechTextList.clear();
|
|
|
|
+ isStop = true;
|
|
|
|
+ speakIndex = 0;
|
|
|
|
+ if (Constants.DEBUG) {
|
|
LogUtil.d(TAG, "stop speak");
|
|
LogUtil.d(TAG, "stop speak");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -121,7 +123,9 @@ public class SpeechUtil {
|
|
public void removeSpeak(String text) {
|
|
public void removeSpeak(String text) {
|
|
synchronized (lockObject) {
|
|
synchronized (lockObject) {
|
|
if (!TextUtils.isEmpty(text) && !TextUtils.isEmpty(speakSpeech)) {
|
|
if (!TextUtils.isEmpty(text) && !TextUtils.isEmpty(speakSpeech)) {
|
|
- LogUtil.d(TAG, "remove speak: " + text);
|
|
|
|
|
|
+ if (Constants.DEBUG) {
|
|
|
|
+ LogUtil.d(TAG, "remove speak: " + text);
|
|
|
|
+ }
|
|
if (text.equals(speakSpeech) && textToSpeech.isSpeaking()) {
|
|
if (text.equals(speakSpeech) && textToSpeech.isSpeaking()) {
|
|
textToSpeech.stop();
|
|
textToSpeech.stop();
|
|
speechTextList.remove(text);
|
|
speechTextList.remove(text);
|
|
@@ -139,15 +143,13 @@ public class SpeechUtil {
|
|
}
|
|
}
|
|
|
|
|
|
public void release() {
|
|
public void release() {
|
|
- synchronized (lockObject) {
|
|
|
|
- speechTextList.clear();
|
|
|
|
- isStop = true;
|
|
|
|
- speakIndex = 0;
|
|
|
|
- if (textToSpeech != null) {
|
|
|
|
- textToSpeech.stop();
|
|
|
|
- textToSpeech.shutdown();
|
|
|
|
- textToSpeech = null;
|
|
|
|
- }
|
|
|
|
|
|
+ speechTextList.clear();
|
|
|
|
+ isStop = true;
|
|
|
|
+ speakIndex = 0;
|
|
|
|
+ if (textToSpeech != null) {
|
|
|
|
+ textToSpeech.stop();
|
|
|
|
+ textToSpeech.shutdown();
|
|
|
|
+ textToSpeech = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -163,17 +165,19 @@ public class SpeechUtil {
|
|
public class SpeechRunnable implements Runnable {
|
|
public class SpeechRunnable implements Runnable {
|
|
public void run() {
|
|
public void run() {
|
|
while (isSpeechLoop) {
|
|
while (isSpeechLoop) {
|
|
- synchronized (lockObject) {
|
|
|
|
|
|
+ //synchronized (lockObject) {
|
|
if (speechTextList.size() > 0 && isStop) {
|
|
if (speechTextList.size() > 0 && isStop) {
|
|
speakSpeech = speechTextList.get(0);
|
|
speakSpeech = speechTextList.get(0);
|
|
- LogUtil.d(TAG, "speakSpeech: " + speakSpeech);
|
|
|
|
|
|
+ if (Constants.DEBUG) {
|
|
|
|
+ LogUtil.d(TAG, "speakSpeech: " + speakSpeech);
|
|
|
|
+ }
|
|
speak(speakSpeech);
|
|
speak(speakSpeech);
|
|
|
|
|
|
//if (speechTextList.contains(speakSpeech)) {
|
|
//if (speechTextList.contains(speakSpeech)) {
|
|
speechTextList.remove(speakSpeech);
|
|
speechTextList.remove(speakSpeech);
|
|
//}
|
|
//}
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ //}
|
|
|
|
|
|
try {
|
|
try {
|
|
Thread.sleep(50);
|
|
Thread.sleep(50);
|