在线朗读采用计算平均朗读速度及时翻页

pull/1336/head
gedoor 3 years ago
parent af12224353
commit f7886df8d1
  1. 1
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  2. 37
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
  3. 4
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.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)

@ -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

@ -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()
}
}

@ -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

Loading…
Cancel
Save