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)