Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Never report audio progress beyond duration #531

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,36 +91,47 @@ public class AudioNavigator<S : Configurable.Settings, P : Configurable.Preferen
.mapNotNull { it.duration }
.takeIf { it.size == readingOrder.items.size }
?.sum()

val coercedOffset =
playback.coerceOffset(currentItem.duration)

val totalProgression =
if (itemStartPosition == null) {
null
} else {
readingOrder.duration?.let { (itemStartPosition + playback.offset) / it }
readingOrder.duration?.let { (itemStartPosition + coercedOffset) / it }
}

val locator = requireNotNull(publication.locatorFromLink(link))
locator.copyWithLocations(
fragments = listOf("t=${playback.offset.inWholeSeconds}"),
progression = item.duration?.let { playback.offset / it },
fragments = listOf("t=${coercedOffset.inWholeSeconds}"),
progression = item.duration?.let { coercedOffset / it },
totalProgression = totalProgression
)
}

override val playback: StateFlow<Playback> =
audioEngine.playback.mapStateIn(coroutineScope) { playback ->
val itemDuration =
readingOrder.items[playback.index].duration

val coercedOffset =
playback.coerceOffset(itemDuration)

Playback(
playback.state.toState(),
playback.playWhenReady,
playback.index,
playback.offset,
coercedOffset,
playback.buffered
)
}

override val location: StateFlow<Location> =
audioEngine.playback.mapStateIn(coroutineScope) {
val currentItem = readingOrder.items[it.index]
Location(currentItem.href, it.offset)
val coercedOffset = it.coerceOffset(currentItem.duration)
Location(currentItem.href, coercedOffset)
}

override fun play() {
Expand Down Expand Up @@ -178,4 +189,11 @@ public class AudioNavigator<S : Configurable.Settings, P : Configurable.Preferen
is AudioEngine.State.Buffering -> State.Buffering
is AudioEngine.State.Failure -> State.Failure(error)
}

private fun AudioEngine.Playback.coerceOffset(duration: Duration?): Duration =
if (duration == null) {
offset
} else {
offset.coerceAtMost(duration)
}
}
Loading