From f7886df8d1d79c25ef1a3d014eea1dda8c76fcaa Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 20 Sep 2021 23:52:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E6=9C=97=E8=AF=BB=E9=87=87?= =?UTF-8?q?=E7=94=A8=E8=AE=A1=E7=AE=97=E5=B9=B3=E5=9D=87=E6=9C=97=E8=AF=BB?= =?UTF-8?q?=E9=80=9F=E5=BA=A6=E5=8F=8A=E6=97=B6=E7=BF=BB=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/service/BaseReadAloudService.kt | 1 + .../app/service/HttpReadAloudService.kt | 37 +++++++++++++------ .../app/ui/book/read/ReadBookActivity.kt | 4 +- .../ui/book/read/page/entities/TextPage.kt | 4 +- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt index ce11ce757..c694a6da1 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -148,6 +148,7 @@ abstract class BaseReadAloudService : BaseService(), open fun resumeReadAloud() { pause = false upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING) + postEvent(EventBus.ALOUD_STATE, Status.PLAY) } abstract fun upSpeechRate(reset: Boolean = false) diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index ea1de1ae5..74ce34867 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -10,10 +10,7 @@ import io.legado.app.model.ReadAloud import io.legado.app.model.ReadBook import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.* -import kotlinx.coroutines.delay -import kotlinx.coroutines.ensureActive -import kotlinx.coroutines.isActive -import kotlinx.coroutines.launch +import kotlinx.coroutines.* import java.io.File import java.io.FileDescriptor import java.io.FileInputStream @@ -31,6 +28,7 @@ class HttpReadAloudService : BaseReadAloudService(), private lateinit var ttsFolder: String private var task: Coroutine<*>? = null private var playingIndex = -1 + private var playIndexJob: Job? = null override fun onCreate() { super.onCreate() @@ -96,6 +94,7 @@ class HttpReadAloudService : BaseReadAloudService(), if (hasSpeakFile(fileName)) { //已经下载好的语音缓存 if (index == nowSpeak) { val file = getSpeakFileAsMd5(fileName) + @Suppress("BlockingMethodInNonBlockingContext") val fis = FileInputStream(file) playAudio(fis.fd) @@ -209,6 +208,7 @@ class HttpReadAloudService : BaseReadAloudService(), override fun pauseReadAloud(pause: Boolean) { super.pauseReadAloud(pause) mediaPlayer.pause() + playIndexJob?.cancel() } override fun resumeReadAloud() { @@ -217,6 +217,26 @@ class HttpReadAloudService : BaseReadAloudService(), play() } else { mediaPlayer.start() + upPlayPos() + } + } + + private fun upPlayPos() { + textChapter?.let { + playIndexJob = launch { + val speakTextLength = contentList[nowSpeak].length + val sleep = mediaPlayer.duration / speakTextLength + val start = speakTextLength * mediaPlayer.currentPosition / mediaPlayer.duration + postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1) + for (i in start..contentList[nowSpeak].length) { + if (readAloudNumber + i > it.getReadLength(pageIndex + 1)) { + pageIndex++ + ReadBook.moveToNextPage() + postEvent(EventBus.TTS_PROGRESS, readAloudNumber + i) + } + delay(sleep.toLong()) + } + } } } @@ -234,13 +254,8 @@ class HttpReadAloudService : BaseReadAloudService(), super.play() if (pause) return mediaPlayer.start() - textChapter?.let { - if (readAloudNumber + 1 > it.getReadLength(pageIndex + 1)) { - pageIndex++ - ReadBook.moveToNextPage() - } - } - postEvent(EventBus.TTS_PROGRESS, readAloudNumber + 1) + playIndexJob?.cancel() + upPlayPos() } private var errorNo = 0 diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 35d0b271a..5e49fb060 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -954,9 +954,9 @@ class ReadBookActivity : ReadBookBaseActivity(), launch(IO) { if (BaseReadAloudService.isPlay()) { ReadBook.curTextChapter?.let { textChapter -> - val pageStart = chapterStart - ReadBook.durChapterPos + val aloudSpanStart = chapterStart - ReadBook.durChapterPos textChapter.getPageByReadPos(ReadBook.durChapterPos) - ?.upPageAloudSpan(pageStart) + ?.upPageAloudSpan(aloudSpanStart) upContent() } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt index ff1808269..0e7dc657f 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt @@ -93,11 +93,11 @@ data class TextPage( return this } - fun upPageAloudSpan(pageStart: Int) { + fun upPageAloudSpan(aloudSpanStart: Int) { removePageAloudSpan() var lineStart = 0 for ((index, textLine) in textLines.withIndex()) { - if (pageStart > lineStart && pageStart < lineStart + textLine.text.length) { + if (aloudSpanStart > lineStart && aloudSpanStart < lineStart + textLine.text.length) { for (i in index - 1 downTo 0) { if (textLines[i].text.endsWith("\n")) { break