瀏覽代碼

TTS语音播报修复语音队列无法销毁的情况

Shangyp 3 月之前
父節點
當前提交
cb2bab5855
共有 1 個文件被更改,包括 10 次插入2 次删除
  1. 10 2
      app/src/main/java/com/hanghui/senic/common/MTTSManager.java

+ 10 - 2
app/src/main/java/com/hanghui/senic/common/MTTSManager.java

@@ -42,8 +42,8 @@ public class MTTSManager implements TextToSpeech.OnInitListener {
             AppLogUtils.e(true, "TTS语音播报", "语音内容为空 ");
             return;
         }
+        AppLogUtils.e(false, "TTS语音播报", "TTS语音播报内容 : " + text);
         if (ismServiceConnectionUsable(mTTS)) {
-            AppLogUtils.e(false, "TTS语音播报", "TTS语音播报内容 : " + text);
             if (mTTS == null) {
                 mTTS = new TextToSpeech(MyAppliction.getContext(), this);
             }
@@ -56,6 +56,10 @@ public class MTTSManager implements TextToSpeech.OnInitListener {
             //TextToSpeech.QUEUE_FLUSH刷新队列,将之前的队列取消阅读现在的文字
             mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null);
         } else {//被回收了不可用状态
+            if (mTTS != null){
+                mTTS.stop();
+                mTTS.shutdown();
+            }
             mTTS = new TextToSpeech(MyAppliction.getContext(), status -> {
                 if (status == TextToSpeech.SUCCESS) {
                     int result = mTTS.setLanguage(Locale.CHINA);
@@ -63,7 +67,11 @@ public class MTTSManager implements TextToSpeech.OnInitListener {
                             && result != TextToSpeech.LANG_AVAILABLE) {
                         AppLogUtils.e(true, " TTS - onInit", "TTS语音播报 onInit: 不支持这种语言");
                     }
-                    mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null, "jiance");
+                    //设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
+                    mTTS.setPitch(1.0f);
+                    //设置语速
+                    mTTS.setSpeechRate(1.0f);
+                    mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null);
                 } else {
                     AppLogUtils.e(true, " TTS - onInit", "TTS语音播报 onInit: TTS初始化失败");
                 }