123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- 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;
- 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 = 2;
- 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.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引擎初始化失败");
- }
- }
- }, "com.iflytek.speechcloud");
- textToSpeech.setSpeechRate(0.5f);
- }
- public void newSpeech(String text, boolean emergency) {
- synchronized (lockObject) {
- if (Constants.DEBUG) {
- LogUtil.d(TAG, "start add text speech: " + text);
- }
- if (speechTextList.contains(text)) {
- return;
- }
- if (Constants.DEBUG) {
- LogUtil.d(TAG, "truely add text speech: " + text);
- }
- if (emergency) {
- speechTextList.add(0, text);
- } else {
- speechTextList.add(text);
- }
- startSpeechThread();
- }
- }
- public synchronized void speak(final String text) {
- if (Constants.DEBUG) {
- LogUtil.d(TAG, "tts speak: " + 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;
- LogUtil.d(TAG, "speak onError..." + utteranceId);
- }
- });
- }
- public void stopSpeak() {
- if (textToSpeech.isSpeaking()) {
- textToSpeech.stop();
- speechTextList.clear();
- isStop = true;
- speakIndex = 0;
- if (Constants.DEBUG) {
- LogUtil.d(TAG, "stop speak");
- }
- }
- }
- public void removeSpeak(String text) {
- synchronized (lockObject) {
- if (!TextUtils.isEmpty(text) && !TextUtils.isEmpty(speakSpeech)) {
- if (Constants.DEBUG) {
- 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);
- }
- }
- }
- }
- 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);
- if (Constants.DEBUG) {
- LogUtil.d(TAG, "speakSpeech: " + speakSpeech);
- }
- speak(speakSpeech);
- //if (speechTextList.contains(speakSpeech)) {
- speechTextList.remove(speakSpeech);
- //}
- }
- //}
- try {
- Thread.sleep(50);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
|