|
@@ -78,44 +78,40 @@ public class MTTSDemo implements TextToSpeech.OnInitListener {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- public void speak(String text, float pitch, float rate, Integer speechQueue) {
|
|
|
|
|
|
+ public synchronized void speak(String text, float pitch, float rate, Integer speechQueue) {
|
|
if (TextUtils.isEmpty(text)) {
|
|
if (TextUtils.isEmpty(text)) {
|
|
AppLogUtils.e(true, "TTS语音播报", "语音内容为空 ");
|
|
AppLogUtils.e(true, "TTS语音播报", "语音内容为空 ");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- if (ismServiceConnectionUsable(mTTS)) {
|
|
|
|
- AppLogUtils.e(false, "TTS语音播报", "TTS语音播报内容 : " + text);
|
|
|
|
- speakContent = text;
|
|
|
|
- this.pitch=pitch;
|
|
|
|
- this.rate=rate;
|
|
|
|
- this.speechQueue=speechQueue;
|
|
|
|
- //设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
|
|
|
|
- mTTS.setPitch(pitch);
|
|
|
|
- //设置语速
|
|
|
|
- mTTS.setSpeechRate(rate);
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- // 设置播报队列方式
|
|
|
|
- if(speechQueue == null){
|
|
|
|
- speechQueue = 0;
|
|
|
|
- }
|
|
|
|
- int mTTsState = TextToSpeech.ERROR;
|
|
|
|
- if (speechQueue == 0) {
|
|
|
|
- // TextToSpeech.QUEUE_ADD添加到队列后面,依次将前面的读完轮序
|
|
|
|
- mTTsState = mTTS.speak(text, TextToSpeech.QUEUE_ADD, null,"hharome");
|
|
|
|
- }else if(speechQueue == 1) {
|
|
|
|
- // TextToSpeech.QUEUE_FLUSH刷新队列,将之前的队列取消阅读现在的文字
|
|
|
|
- mTTsState = mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null,"hharome");
|
|
|
|
- }
|
|
|
|
- if(mTTsState != TextToSpeech.SUCCESS){
|
|
|
|
- reset(text);
|
|
|
|
- }
|
|
|
|
- } else {//被回收了不可用状态
|
|
|
|
- reset(text);
|
|
|
|
|
|
+ AppLogUtils.e(false, "TTS语音播报", "TTS语音播报内容 : " + text);
|
|
|
|
+ speakContent = text;
|
|
|
|
+ this.pitch=pitch;
|
|
|
|
+ this.rate=rate;
|
|
|
|
+ this.speechQueue=speechQueue;
|
|
|
|
+ //设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
|
|
|
|
+ mTTS.setPitch(pitch);
|
|
|
|
+ //设置语速
|
|
|
|
+ mTTS.setSpeechRate(rate);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // 设置播报队列方式
|
|
|
|
+ if(speechQueue == null){
|
|
|
|
+ speechQueue = 0;
|
|
|
|
+ }
|
|
|
|
+ int mTTsState = TextToSpeech.ERROR;
|
|
|
|
+ if (speechQueue == 0) {
|
|
|
|
+ // TextToSpeech.QUEUE_ADD添加到队列后面,依次将前面的读完轮序
|
|
|
|
+ mTTsState = mTTS.speak(text, TextToSpeech.QUEUE_ADD, null,"hharome");
|
|
|
|
+ }else if(speechQueue == 1) {
|
|
|
|
+ // TextToSpeech.QUEUE_FLUSH刷新队列,将之前的队列取消阅读现在的文字
|
|
|
|
+ mTTsState = mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null,"hharome");
|
|
|
|
+ }
|
|
|
|
+ if(mTTsState != TextToSpeech.SUCCESS){
|
|
|
|
+ reset(text,speechQueue);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private void reset(String text){
|
|
|
|
|
|
+ private void reset(String text,final int speechQueue){
|
|
if (mTTS != null){
|
|
if (mTTS != null){
|
|
mTTS.stop();
|
|
mTTS.stop();
|
|
mTTS.shutdown();
|
|
mTTS.shutdown();
|
|
@@ -127,7 +123,14 @@ public class MTTSDemo implements TextToSpeech.OnInitListener {
|
|
&& result != TextToSpeech.LANG_AVAILABLE) {
|
|
&& result != TextToSpeech.LANG_AVAILABLE) {
|
|
AppLogUtils.e(true, " TTS - onInit", "TTS语音播报 onInit: 不支持这种语言");
|
|
AppLogUtils.e(true, " TTS - onInit", "TTS语音播报 onInit: 不支持这种语言");
|
|
}
|
|
}
|
|
- mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null, "hharome");
|
|
|
|
|
|
+
|
|
|
|
+ if (0 == speechQueue) {
|
|
|
|
+ // TextToSpeech.QUEUE_ADD添加到队列后面,依次将前面的读完轮序
|
|
|
|
+ mTTS.speak(text, TextToSpeech.QUEUE_ADD, null,"hharome");
|
|
|
|
+ }else if(speechQueue == 1) {
|
|
|
|
+ // TextToSpeech.QUEUE_FLUSH刷新队列,将之前的队列取消阅读现在的文字
|
|
|
|
+ mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null,"hharome");
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
AppLogUtils.e(true, " TTS - onInit", "TTS语音播报 onInit: TTS初始化失败");
|
|
AppLogUtils.e(true, " TTS - onInit", "TTS语音播报 onInit: TTS初始化失败");
|
|
}
|
|
}
|
|
@@ -150,6 +153,12 @@ public class MTTSDemo implements TextToSpeech.OnInitListener {
|
|
//不支持就改成英文
|
|
//不支持就改成英文
|
|
mTTS.setLanguage(Locale.US);
|
|
mTTS.setLanguage(Locale.US);
|
|
}
|
|
}
|
|
|
|
+ int mTTsState = TextToSpeech.ERROR;
|
|
|
|
+ mTTsState = mTTS.speak("", TextToSpeech.QUEUE_FLUSH, null,"hharome");
|
|
|
|
+ if(mTTsState != TextToSpeech.SUCCESS){
|
|
|
|
+ reset("",speechQueue);
|
|
|
|
+ }
|
|
|
|
+
|
|
} else {
|
|
} else {
|
|
// 检查语音数据是否已下载
|
|
// 检查语音数据是否已下载
|
|
if (mTTS.isLanguageAvailable(Locale.CHINESE) >= TextToSpeech.LANG_AVAILABLE) {
|
|
if (mTTS.isLanguageAvailable(Locale.CHINESE) >= TextToSpeech.LANG_AVAILABLE) {
|