From 6637a4b78b5c66f2b9dab5e63dba8ad9502bd881 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 18 Jul 2023 08:26:23 +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 --- .../legado/app/service/TTSReadAloudService.kt | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) 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 3abcddf7e..34ca1b37e 100644 --- a/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt @@ -86,7 +86,12 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener speakJob?.cancel() speakJob = execute { val tts = textToSpeech ?: throw NoStackTraceException("tts is null") - var result = tts.speak("", TextToSpeech.QUEUE_FLUSH, null, null) + var result = tts.runCatching { + speak("", TextToSpeech.QUEUE_FLUSH, null, null) + }.getOrElse { + AppLog.put("tts出错\n${it.localizedMessage}", it, true) + TextToSpeech.ERROR + } if (result == TextToSpeech.ERROR) { clearTTS() initTts() @@ -99,7 +104,12 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener if (text.matches(AppPattern.notReadAloudRegex)) { continue } - result = tts.speak(text, TextToSpeech.QUEUE_ADD, null, AppConst.APP_TAG + i) + result = tts.runCatching { + speak(text, TextToSpeech.QUEUE_ADD, null, AppConst.APP_TAG + i) + }.getOrElse { + AppLog.put("tts出错\n${it.localizedMessage}", it, true) + TextToSpeech.ERROR + } if (result == TextToSpeech.ERROR) { AppLog.put("tts朗读出错:$text") } @@ -111,7 +121,9 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener } override fun playStop() { - textToSpeech?.stop() + textToSpeech?.runCatching { + stop() + } } /** @@ -135,7 +147,9 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener override fun pauseReadAloud(abandonFocus: Boolean) { super.pauseReadAloud(abandonFocus) speakJob?.cancel() - textToSpeech?.stop() + textToSpeech?.runCatching { + stop() + } } /**