From f074e8bf2f3865ed7db9cf720d4de07d69d92ffd Mon Sep 17 00:00:00 2001 From: sheng-ri <38691071+sheng-ri@users.noreply.github.com> Date: Sat, 14 Sep 2024 11:34:55 +0800 Subject: [PATCH] fix-empty-tts-text (#4199) --- .../legado/app/service/TTSReadAloudService.kt | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 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 c2f6a1a36..aa3e3962b 100644 --- a/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/TTSReadAloudService.kt @@ -93,37 +93,41 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener LogUtils.d(TAG, "朗读列表大小 ${contentList.size}") LogUtils.d(TAG, "朗读页数 ${textChapter?.pageSize}") val tts = textToSpeech ?: throw NoStackTraceException("tts is 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) { - AppLog.put("tts出错 尝试重新初始化") - clearTTS() - initTts() - return@execute - } - val contentList = contentList for (i in nowSpeak until contentList.size) { - ensureActive() var text = contentList[i] + ensureActive() if (paragraphStartPos > 0 && i == nowSpeak) { text = text.substring(paragraphStartPos) } if (text.matches(AppPattern.notReadAloudRegex)) { continue } - 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") + if (i == nowSpeak) { + val result = tts.runCatching { + speak(text, TextToSpeech.QUEUE_FLUSH, null, AppConst.APP_TAG + i) + }.getOrElse { + AppLog.put("tts出错\n${it.localizedMessage}", it, true) + TextToSpeech.ERROR + } + if (result == TextToSpeech.ERROR) { + AppLog.put("tts出错 尝试重新初始化") + clearTTS() + initTts() + return@execute + } + } else { + val 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") + } } + + } LogUtils.d(TAG, "朗读内容添加完成") }.onError {