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 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(); } } } } }