diff --git a/readium/navigators/media/audio/src/main/java/org/readium/navigator/media/audio/AudioNavigator.kt b/readium/navigators/media/audio/src/main/java/org/readium/navigator/media/audio/AudioNavigator.kt index 0b1f7e5c6e..e5bb2abc43 100644 --- a/readium/navigators/media/audio/src/main/java/org/readium/navigator/media/audio/AudioNavigator.kt +++ b/readium/navigators/media/audio/src/main/java/org/readium/navigator/media/audio/AudioNavigator.kt @@ -91,28 +91,38 @@ public class AudioNavigator = 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 ) } @@ -120,7 +130,8 @@ public class AudioNavigator = 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() { @@ -178,4 +189,11 @@ public class AudioNavigator 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) + } }