Просмотр исходного кода

Merge pull request #2260 from 821938089/little-fix

优化TTS
kunfei 4 лет назад
Родитель
Сommit
dd0565f021
1 измененных файлов с 20 добавлено и 4 удалено
  1. 20 4
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

+ 20 - 4
app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@@ -77,6 +77,7 @@ class HttpReadAloudService : BaseReadAloudService(),
                 }
                 }
             }.onFailure {
             }.onFailure {
                 toastOnUi("朗读出错:${it.localizedMessage}")
                 toastOnUi("朗读出错:${it.localizedMessage}")
+                AppLog.put("朗读出错:${it.localizedMessage}", it)
             }
             }
         }
         }
     }
     }
@@ -115,7 +116,12 @@ class HttpReadAloudService : BaseReadAloudService(),
                             playAudio(file)
                             playAudio(file)
                         }
                         }
                     } else if (speakText.isEmpty()) {
                     } else if (speakText.isEmpty()) {
+                        AppLog.put("阅读段落内容为空,使用无声音频代替。")
                         createSilentSound(fileName)
                         createSilentSound(fileName)
+                        if (index == nowSpeak) {
+                            val file = getSpeakFileAsMd5(fileName)
+                            playAudio(file)
+                        }
                         return@forEachIndexed
                         return@forEachIndexed
                     } else {
                     } else {
                         runCatching {
                         runCatching {
@@ -138,7 +144,7 @@ class HttpReadAloudService : BaseReadAloudService(),
                             }?.let { ct ->
                             }?.let { ct ->
                                 response.headers["Content-Type"]?.let { contentType ->
                                 response.headers["Content-Type"]?.let { contentType ->
                                     if (!contentType.matches(ct.toRegex())) {
                                     if (!contentType.matches(ct.toRegex())) {
-                                        throw NoStackTraceException(response.body!!.string())
+                                        throw NoStackTraceException("TTS服务器返回错误:" + response.body!!.string())
                                     }
                                     }
                                 }
                                 }
                             }
                             }
@@ -182,9 +188,16 @@ class HttpReadAloudService : BaseReadAloudService(),
                                     AppLog.put(msg, it)
                                     AppLog.put(msg, it)
                                     it.printOnDebug()
                                     it.printOnDebug()
                                     if (downloadErrorNo > 5) {
                                     if (downloadErrorNo > 5) {
+                                        AppLog.put("TTS服务器连续5次错误,已暂停阅读。")
+                                        toastOnUi("TTS服务器连续5次错误,已暂停阅读。")
                                         pauseReadAloud(true)
                                         pauseReadAloud(true)
                                     } else {
                                     } else {
+                                        AppLog.put("TTS下载音频出错,使用无声音频代替。")
                                         createSilentSound(fileName)
                                         createSilentSound(fileName)
+                                        if (index == nowSpeak) {
+                                            val file = getSpeakFileAsMd5(fileName)
+                                            playAudio(file)
+                                        }
                                     }
                                     }
                                 }
                                 }
                             }
                             }
@@ -194,7 +207,7 @@ class HttpReadAloudService : BaseReadAloudService(),
             }.onStart {
             }.onStart {
                 downloadTaskIsActive = true
                 downloadTaskIsActive = true
             }.onError {
             }.onError {
-                AppLog.put("朗读下载出错\n${it.localizedMessage}")
+                AppLog.put("朗读下载出错\n${it.localizedMessage}", it)
             }.onFinally {
             }.onFinally {
                 downloadTaskIsActive = false
                 downloadTaskIsActive = false
             }
             }
@@ -245,8 +258,10 @@ class HttpReadAloudService : BaseReadAloudService(),
     private fun removeCacheFile() {
     private fun removeCacheFile() {
         val titleMd5 = MD5Utils.md5Encode16(textChapter?.title ?: "")
         val titleMd5 = MD5Utils.md5Encode16(textChapter?.title ?: "")
         FileUtils.listDirsAndFiles(ttsFolderPath)?.forEach {
         FileUtils.listDirsAndFiles(ttsFolderPath)?.forEach {
-            if (!it.name.startsWith(titleMd5)
-                && System.currentTimeMillis() - it.lastModified() > 600000
+            val isSilentSound = it.length() == 2160L
+            if ((!it.name.startsWith(titleMd5)
+                        && System.currentTimeMillis() - it.lastModified() > 600000)
+                || isSilentSound
             ) {
             ) {
                 FileUtils.delete(it.absolutePath)
                 FileUtils.delete(it.absolutePath)
             }
             }
@@ -334,6 +349,7 @@ class HttpReadAloudService : BaseReadAloudService(),
         playErrorNo++
         playErrorNo++
         if (playErrorNo >= 5) {
         if (playErrorNo >= 5) {
             toastOnUi("朗读连续5次错误, 最后一次错误代码(${error.localizedMessage})")
             toastOnUi("朗读连续5次错误, 最后一次错误代码(${error.localizedMessage})")
+            AppLog.put("朗读连续5次错误, 最后一次错误代码(${error.localizedMessage})", error)
             ReadAloud.pause(this)
             ReadAloud.pause(this)
         } else {
         } else {
             playNext()
             playNext()