This commit is contained in:
Horis 2024-03-05 17:01:41 +08:00
parent 752999b43b
commit c562c40689
3 changed files with 20 additions and 13 deletions

View File

@ -297,14 +297,12 @@ object ReadBook : CoroutineScope by MainScope() {
fun recycleRecorders(beforeIndex: Int, afterIndex: Int) { fun recycleRecorders(beforeIndex: Int, afterIndex: Int) {
executor.execute { executor.execute {
val textChapter = curTextChapter val textChapter = curTextChapter ?: return@execute
if (textChapter != null) { if (afterIndex > beforeIndex) {
if (afterIndex > beforeIndex) { textChapter.getPage(afterIndex - 2)?.recycleRecorders()
textChapter.getPage(afterIndex - 2)?.recycleRecorders() }
} if (afterIndex < beforeIndex) {
if (afterIndex < beforeIndex) { textChapter.getPage(afterIndex + 3)?.recycleRecorders()
textChapter.getPage(afterIndex + 3)?.recycleRecorders()
}
} }
} }
} }

View File

@ -314,8 +314,8 @@ class HttpReadAloudService : BaseReadAloudService(),
pageIndex++ pageIndex++
if (pageIndex < textChapter.pageSize) { if (pageIndex < textChapter.pageSize) {
ReadBook.moveToNextPage() ReadBook.moveToNextPage()
upTtsProgress(readAloudNumber + i.toInt())
} }
upTtsProgress(readAloudNumber + i.toInt())
} }
delay(sleep) delay(sleep)
} }
@ -360,6 +360,9 @@ class HttpReadAloudService : BaseReadAloudService(),
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) { override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
if (reason == Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED) return if (reason == Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED) return
if (reason == Player.MEDIA_ITEM_TRANSITION_REASON_AUTO) {
playErrorNo = 0
}
updateNextPos() updateNextPos()
upPlayPos() upPlayPos()
} }
@ -371,13 +374,19 @@ class HttpReadAloudService : BaseReadAloudService(),
if (playErrorNo >= 5) { if (playErrorNo >= 5) {
toastOnUi("朗读连续5次错误, 最后一次错误代码(${error.localizedMessage})") toastOnUi("朗读连续5次错误, 最后一次错误代码(${error.localizedMessage})")
AppLog.put("朗读连续5次错误, 最后一次错误代码(${error.localizedMessage})", error) AppLog.put("朗读连续5次错误, 最后一次错误代码(${error.localizedMessage})", error)
ReadAloud.pause(this) pauseReadAloud()
} else { } else {
updateNextPos() deleteCurrentSpeakFile()
exoPlayer.seekToNextMediaItem() downloadAndPlayAudios()
} }
} }
private fun deleteCurrentSpeakFile() {
val mediaItem = exoPlayer.currentMediaItem ?: return
val filePath = mediaItem.localConfiguration!!.uri.path!!
File(filePath).delete()
}
override fun aloudServicePendingIntent(actionStr: String): PendingIntent? { override fun aloudServicePendingIntent(actionStr: String): PendingIntent? {
return servicePendingIntent<HttpReadAloudService>(actionStr) return servicePendingIntent<HttpReadAloudService>(actionStr)
} }

View File

@ -194,7 +194,7 @@ data class TextPage(
for (index in textLines.indices) { for (index in textLines.indices) {
val textLine = textLines[index] val textLine = textLines[index]
val lineLength = textLine.text.length + if (textLine.isParagraphEnd) 1 else 0 val lineLength = textLine.text.length + if (textLine.isParagraphEnd) 1 else 0
if (aloudSpanStart > lineStart && aloudSpanStart < lineStart + lineLength) { if (aloudSpanStart >= lineStart && aloudSpanStart < lineStart + lineLength) {
for (i in index - 1 downTo 0) { for (i in index - 1 downTo 0) {
if (textLines[i].isParagraphEnd) { if (textLines[i].isParagraphEnd) {
break break