From ad164692107b9429b5c9f7017f53536e046200d8 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 5 Feb 2022 11:13:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/service/HttpReadAloudService.kt | 7 +++++-- .../io/legado/app/service/TTSReadAloudService.kt | 12 ++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index db06ea216..3b2dbd588 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -68,9 +68,10 @@ class HttpReadAloudService : BaseReadAloudService(), ReadBook.readAloud() } else { super.play() - ReadAloud.httpTTS?.let { + kotlin.runCatching { + val tts = ReadAloud.httpTTS ?: throw NoStackTraceException("httpTts is null") val fileName = - md5SpeakFileName(it.url, AppConfig.ttsSpeechRate.toString(), contentList[nowSpeak]) + md5SpeakFileName(tts.url, AppConfig.ttsSpeechRate.toString(), contentList[nowSpeak]) if (nowSpeak == 0) { downloadAudio() } else { @@ -81,6 +82,8 @@ class HttpReadAloudService : BaseReadAloudService(), downloadAudio() } } + }.onFailure { + toastOnUi("朗读出错:${it.localizedMessage}") } } } diff --git a/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt b/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt index 0bd8b96cc..13b251143 100644 --- a/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt @@ -11,6 +11,7 @@ import io.legado.app.constant.EventBus import io.legado.app.help.AppConfig import io.legado.app.help.MediaHelp import io.legado.app.lib.dialogs.SelectItem +import io.legado.app.model.NoStackTraceException import io.legado.app.model.ReadBook import io.legado.app.utils.* import java.util.* @@ -75,12 +76,11 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener super.play() kotlin.runCatching { MediaHelp.playSilentSound(this@TTSReadAloudService) - textToSpeech!!.let { - it.speak("", TextToSpeech.QUEUE_FLUSH, null, null) - for (i in nowSpeak until contentList.size) { - val text = contentList[i].replace(AppPattern.notReadAloudRegex, "") - it.speak(text, TextToSpeech.QUEUE_ADD, null, AppConst.APP_TAG + i) - } + val tts = textToSpeech ?: throw NoStackTraceException("tts is null") + tts.speak("", TextToSpeech.QUEUE_FLUSH, null, null) + for (i in nowSpeak until contentList.size) { + val text = contentList[i].replace(AppPattern.notReadAloudRegex, "") + tts.speak(text, TextToSpeech.QUEUE_ADD, null, AppConst.APP_TAG + i) } }.onFailure { AppLog.put("tts朗读出错", it)