From c083c8cb599729c38202e2a77dcca3f225deb114 Mon Sep 17 00:00:00 2001 From: Freya Arbjerg Date: Mon, 9 Oct 2023 13:59:21 +0200 Subject: [PATCH] Return negative position when there is no track Also coerce position up to the length --- .../dev/schlaubi/lavakord/audio/internal/WebsocketPlayer.kt | 3 ++- .../kotlin/dev/schlaubi/lavakord/audio/player/Player.kt | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/WebsocketPlayer.kt b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/WebsocketPlayer.kt index d1e9abe2..b3a0b7fc 100644 --- a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/WebsocketPlayer.kt +++ b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/WebsocketPlayer.kt @@ -29,10 +29,11 @@ internal class WebsocketPlayer(internal val node: NodeImpl, internal val guildId private var updateTime: Instant = Instant.DISTANT_PAST override val positionDuration: Duration get() { + val trackLength = playingTrack?.info?.length?.milliseconds ?: return (-1).milliseconds val now = Clock.System.now() val elapsedSinceUpdate = now - updateTime - return lastPosition + elapsedSinceUpdate + return (lastPosition + elapsedSinceUpdate).coerceAtLeast(trackLength) } override val volume: Int diff --git a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/player/Player.kt b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/player/Player.kt index c9b90c60..8ce6ed30 100644 --- a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/player/Player.kt +++ b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/player/Player.kt @@ -18,8 +18,10 @@ import kotlin.time.DurationUnit * @property playingTrack the currently playing [Track] * @property paused whether the playback is currently paused or not * @property volume the current volume of this player - * @property position the position of the current song the player is at (-1 if [playingTrack] is null) - * @property positionDuration the position of the current song the player is at (-1 if [playingTrack] is null) + * @property position the position of the current song the player is at (or negative if [playingTrack] is null). + * Capped at the track length. + * @property positionDuration the position of the current song the player is at (or negative if [playingTrack] is null). + * Capped at the track length. * @property equalizers the applied equalizers in this player */ public interface Player : EventSource {