Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Nov 23, 2024
1 parent db16345 commit 48801b8
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/src/main/java/io/legado/app/service/TTSReadAloudService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.legado.app.utils.LogUtils
import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.servicePendingIntent
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive

/**
Expand Down Expand Up @@ -94,6 +95,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
LogUtils.d(TAG, "朗读页数 ${textChapter?.pageSize}")
val tts = textToSpeech ?: throw NoStackTraceException("tts is null")
val contentList = contentList
var isAddedText = false
for (i in nowSpeak until contentList.size) {
ensureActive()
var text = contentList[i]
Expand All @@ -103,7 +105,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
if (text.matches(AppPattern.notReadAloudRegex)) {
continue
}
if (i == nowSpeak) {
if (!isAddedText) {
val result = tts.runCatching {
speak(text, TextToSpeech.QUEUE_FLUSH, null, AppConst.APP_TAG + i)
}.getOrElse {
Expand All @@ -127,8 +129,14 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
AppLog.put("tts朗读出错:$text")
}
}
isAddedText = true
}
LogUtils.d(TAG, "朗读内容添加完成")
if (!isAddedText) {
playStop()
delay(1000)
nextChapter()
}
}.onError {
AppLog.put("tts朗读出错\n${it.localizedMessage}", it, true)
}
Expand Down Expand Up @@ -184,6 +192,9 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
override fun onStart(s: String) {
LogUtils.d(TAG, "onStart nowSpeak:$nowSpeak pageIndex:$pageIndex utteranceId:$s")
textChapter?.let {
if (contentList[nowSpeak].matches(AppPattern.notReadAloudRegex)) {
nextParagraph()
}
if (readAloudNumber + 1 > it.getReadLength(pageIndex + 1)) {
pageIndex++
ReadBook.moveToNextPage()
Expand Down

0 comments on commit 48801b8

Please sign in to comment.