Skip to content

Commit

Permalink
fix-empty-tts-text (#4199)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheng-ri authored Sep 14, 2024
1 parent d51d0f8 commit f074e8b
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions app/src/main/java/io/legado/app/service/TTSReadAloudService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f074e8b

Please sign in to comment.