Эх сурвалжийг харах

3.0 optimize:TTS语音优化

xwh 1 сар өмнө
parent
commit
38250915e9

+ 2 - 2
config.gradle

@@ -7,8 +7,8 @@ ext {
 //            minSdkVersion    : 24,
             minSdkVersion    : 21,
             targetSdkVersion : 22,
-            versionCode      : 79,
-            versionName      : "3.1.0.241129"
+            versionCode      : 80,
+            versionName      : "3.1.1.241206"
     ]
     //依赖 配置
     dependencies = [

+ 41 - 32
lib_tts/src/main/java/com/hh/lib_tts/voice/MTTSDemo.java

@@ -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)) {
             AppLogUtils.e(true, "TTS语音播报", "语音内容为空 ");
             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){
             mTTS.stop();
             mTTS.shutdown();
@@ -127,7 +123,14 @@ public class MTTSDemo implements TextToSpeech.OnInitListener {
                         && result != TextToSpeech.LANG_AVAILABLE) {
                     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 {
                 AppLogUtils.e(true, " TTS - onInit", "TTS语音播报 onInit: TTS初始化失败");
             }
@@ -150,6 +153,12 @@ public class MTTSDemo implements TextToSpeech.OnInitListener {
                 //不支持就改成英文
                 mTTS.setLanguage(Locale.US);
             }
+            int mTTsState = TextToSpeech.ERROR;
+            mTTsState = mTTS.speak("", TextToSpeech.QUEUE_FLUSH, null,"hharome");
+            if(mTTsState != TextToSpeech.SUCCESS){
+                reset("",speechQueue);
+            }
+
         } else {
             // 检查语音数据是否已下载
             if (mTTS.isLanguageAvailable(Locale.CHINESE) >= TextToSpeech.LANG_AVAILABLE) {