This commit is contained in:
@@ -741,9 +741,14 @@ class ReaderActivity : BaseActivity() {
|
||||
if (ttsState.playing) {
|
||||
ReaderTtsController.toggle(this)
|
||||
} else {
|
||||
val currentPageIndex = (state.currentPage - 1).coerceAtLeast(0)
|
||||
if (currentPageIndex != ttsState.readerPageIndex) {
|
||||
ReaderTtsController.resumeFromPage(this, currentPageIndex)
|
||||
val currentPage = chapter.pages
|
||||
?.getOrNull((state.currentPage - 1).coerceAtLeast(0)) as? TextReaderPage
|
||||
if (currentPage != null) {
|
||||
ReaderTtsController.resumeFromSource(
|
||||
context = this,
|
||||
documentIndex = currentPage.anchor.documentIndex,
|
||||
sourceOffset = currentPage.anchor.startOffset,
|
||||
)
|
||||
} else {
|
||||
ReaderTtsController.toggle(this)
|
||||
}
|
||||
|
||||
@@ -57,12 +57,17 @@ object ReaderTtsController {
|
||||
)
|
||||
}
|
||||
|
||||
fun resumeFromPage(context: Context, readerPageIndex: Int) {
|
||||
fun resumeFromSource(
|
||||
context: Context,
|
||||
documentIndex: Int,
|
||||
sourceOffset: Long,
|
||||
) {
|
||||
if (!state.value.active) return
|
||||
context.startService(
|
||||
Intent(context, ReaderTtsService::class.java)
|
||||
.setAction(ReaderTtsService.ACTION_RESUME_FROM_PAGE)
|
||||
.putExtra(ReaderTtsService.EXTRA_START_PAGE_INDEX, readerPageIndex),
|
||||
.setAction(ReaderTtsService.ACTION_RESUME_FROM_SOURCE)
|
||||
.putExtra(ReaderTtsService.EXTRA_DOCUMENT_INDEX, documentIndex)
|
||||
.putExtra(ReaderTtsService.EXTRA_SOURCE_OFFSET, sourceOffset),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -46,8 +46,9 @@ class ReaderTtsService : Service() {
|
||||
when (intent?.action) {
|
||||
ACTION_START -> startSession(intent)
|
||||
ACTION_TOGGLE -> if (playing) pausePlayback() else resumePlayback()
|
||||
ACTION_RESUME_FROM_PAGE -> resumeFromPage(
|
||||
intent.getIntExtra(EXTRA_START_PAGE_INDEX, -1),
|
||||
ACTION_RESUME_FROM_SOURCE -> resumeFromSource(
|
||||
documentIndex = intent.getIntExtra(EXTRA_DOCUMENT_INDEX, -1),
|
||||
sourceOffset = intent.getLongExtra(EXTRA_SOURCE_OFFSET, -1L),
|
||||
)
|
||||
ACTION_STOP -> stopPlayback()
|
||||
ACTION_SET_SPEED -> {
|
||||
@@ -207,16 +208,47 @@ class ReaderTtsService : Service() {
|
||||
speakCurrentSegment()
|
||||
}
|
||||
|
||||
private fun resumeFromPage(readerPageIndex: Int) {
|
||||
if (!initialized || readerPageIndex < 0) return
|
||||
private fun resumeFromSource(
|
||||
documentIndex: Int,
|
||||
sourceOffset: Long,
|
||||
) {
|
||||
if (!initialized || documentIndex < 0 || sourceOffset < 0L) return
|
||||
val session = session ?: return
|
||||
val newPagePosition = session.pages.indexOfFirst { it.readerPageIndex >= readerPageIndex }
|
||||
.takeIf { it >= 0 }
|
||||
?: return
|
||||
|
||||
val exactPosition = session.pages.indexOfFirst { entry ->
|
||||
entry.documentIndex == documentIndex &&
|
||||
sourceOffset >= entry.sourceStartOffset &&
|
||||
sourceOffset < entry.sourceEndOffsetExclusive
|
||||
}
|
||||
val nextPosition = if (exactPosition < 0) {
|
||||
session.pages.indexOfFirst { entry ->
|
||||
entry.documentIndex == documentIndex && entry.sourceStartOffset >= sourceOffset
|
||||
}
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
val previousPosition = if (exactPosition < 0 && nextPosition < 0) {
|
||||
session.pages.indexOfLast { entry ->
|
||||
entry.documentIndex == documentIndex && entry.sourceStartOffset <= sourceOffset
|
||||
}
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
val newPagePosition = when {
|
||||
exactPosition >= 0 -> exactPosition
|
||||
nextPosition >= 0 -> nextPosition
|
||||
previousPosition >= 0 -> previousPosition
|
||||
else -> return
|
||||
}
|
||||
val entry = session.pages[newPagePosition]
|
||||
val relativeOffset = (sourceOffset - entry.sourceStartOffset)
|
||||
.coerceAtLeast(0L)
|
||||
.coerceAtMost(Int.MAX_VALUE.toLong())
|
||||
.toInt()
|
||||
|
||||
tts?.stop()
|
||||
pagePosition = newPagePosition
|
||||
pageSpeechStartOffset = 0
|
||||
pageSpeechStartOffset = relativeOffset
|
||||
currentWindow = null
|
||||
currentSegments = emptyList()
|
||||
lookaheadConsumedChars = emptyMap()
|
||||
@@ -225,7 +257,18 @@ class ReaderTtsService : Service() {
|
||||
currentUtteranceBaseOffset = 0
|
||||
currentUtteranceId = null
|
||||
playing = true
|
||||
updatePlaybackState(clearHighlight = true)
|
||||
ReaderTtsController.updateState(
|
||||
ReaderTtsController.State(
|
||||
active = true,
|
||||
playing = true,
|
||||
readerChapterId = readerChapterId,
|
||||
readerPageIndex = entry.readerPageIndex,
|
||||
documentIndex = documentIndex,
|
||||
sourceSentenceStart = sourceOffset,
|
||||
sourceSentenceEndExclusive = sourceOffset,
|
||||
),
|
||||
)
|
||||
refreshNotification()
|
||||
speakCurrentSegment()
|
||||
}
|
||||
|
||||
@@ -721,7 +764,7 @@ class ReaderTtsService : Service() {
|
||||
companion object {
|
||||
const val ACTION_START = "eu.kanade.tachiyomi.reader.tts.START"
|
||||
const val ACTION_TOGGLE = "eu.kanade.tachiyomi.reader.tts.TOGGLE"
|
||||
const val ACTION_RESUME_FROM_PAGE = "eu.kanade.tachiyomi.reader.tts.RESUME_FROM_PAGE"
|
||||
const val ACTION_RESUME_FROM_SOURCE = "eu.kanade.tachiyomi.reader.tts.RESUME_FROM_SOURCE"
|
||||
const val ACTION_STOP = "eu.kanade.tachiyomi.reader.tts.STOP"
|
||||
const val ACTION_SET_SPEED = "eu.kanade.tachiyomi.reader.tts.SET_SPEED"
|
||||
const val ACTION_SET_ENGINE = "eu.kanade.tachiyomi.reader.tts.SET_ENGINE"
|
||||
@@ -729,6 +772,8 @@ class ReaderTtsService : Service() {
|
||||
|
||||
const val EXTRA_SESSION_ID = "session_id"
|
||||
const val EXTRA_START_PAGE_INDEX = "start_page_index"
|
||||
const val EXTRA_DOCUMENT_INDEX = "document_index"
|
||||
const val EXTRA_SOURCE_OFFSET = "source_offset"
|
||||
const val EXTRA_CHAPTER_ID = "chapter_id"
|
||||
const val EXTRA_TITLE = "title"
|
||||
const val EXTRA_SUBTITLE = "subtitle"
|
||||
|
||||
Reference in New Issue
Block a user