Skip to content

Commit 23595ab

Browse files
committed
feat: add duration property to VideoPlayerState
Expose the total media duration in seconds via a new `duration` property on VideoPlayerState, complementing the existing `currentTime` property. Closes #94
1 parent 47044e6 commit 23595ab

8 files changed

Lines changed: 24 additions & 0 deletions

File tree

mediaplayer/src/androidMain/kotlin/io/github/kdroidfilter/composemediaplayer/VideoPlayerState.android.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ actual fun createVideoPlayerState(audioMode: AudioMode): VideoPlayerState =
5858
positionText = "00:00",
5959
durationText = "00:00",
6060
currentTime = 0.0,
61+
duration = 0.0,
6162
isFullscreen = false,
6263
aspectRatio = 16f / 9f,
6364
error =
@@ -267,6 +268,7 @@ open class DefaultVideoPlayerState(
267268
override val positionText: String get() = formatTime(_currentTime)
268269
override val durationText: String get() = formatTime(_duration)
269270
override val currentTime: Double get() = _currentTime
271+
override val duration: Double get() = _duration
270272

271273
override val isPipSupported: Boolean
272274
get() {

mediaplayer/src/commonMain/kotlin/io/github/kdroidfilter/composemediaplayer/VideoPlayerState.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ interface VideoPlayerState {
7373
*/
7474
val durationText: String
7575
val currentTime: Double
76+
77+
/**
78+
* Returns the total duration of the media in seconds.
79+
*/
80+
val duration: Double
7681
var isFullscreen: Boolean
7782
val aspectRatio: Float
7883

@@ -190,6 +195,7 @@ data class PreviewableVideoPlayerState(
190195
override val positionText: String = "00:05",
191196
override val durationText: String = "00:10",
192197
override val currentTime: Double = 5000.0,
198+
override val duration: Double = 10.0,
193199
override var isFullscreen: Boolean = false,
194200
override val aspectRatio: Float = 1.7f,
195201
override val error: VideoPlayerError? = null,

mediaplayer/src/iosMain/kotlin/io/github/kdroidfilter/composemediaplayer/VideoPlayerState.ios.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ open class DefaultVideoPlayerState(
169169
private var _currentTime: Double = 0.0
170170
private var _duration: Double = 0.0
171171
override val currentTime: Double get() = _currentTime
172+
override val duration: Double get() = _duration
172173

173174
// Observable video aspect ratio (default to 16:9)
174175
private var _videoAspectRatio by mutableStateOf(16.0 / 9.0)

mediaplayer/src/jvmMain/kotlin/io/github/kdroidfilter/composemediaplayer/VideoPlayerState.jvm.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ open class DefaultVideoPlayerState : VideoPlayerState {
103103
override val positionText: String get() = delegate.positionText
104104
override val durationText: String get() = delegate.durationText
105105
override val currentTime: Double get() = delegate.currentTime
106+
override val duration: Double get() = delegate.duration
106107

107108
override fun openUri(
108109
uri: String,

mediaplayer/src/jvmMain/kotlin/io/github/kdroidfilter/composemediaplayer/linux/LinuxVideoPlayerState.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ class LinuxVideoPlayerState : VideoPlayerState {
112112
if (hasMedia) getPositionSafely() else 0.0
113113
}
114114

115+
override val duration: Double
116+
get() =
117+
runBlocking {
118+
if (hasMedia) getDurationSafely() else 0.0
119+
}
120+
115121
private val _aspectRatio = mutableStateOf(16f / 9f)
116122
override val aspectRatio: Float get() = _aspectRatio.value
117123

mediaplayer/src/jvmMain/kotlin/io/github/kdroidfilter/composemediaplayer/mac/MacVideoPlayerState.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ class MacVideoPlayerState : VideoPlayerState {
110110
if (hasMedia) getPositionSafely() else 0.0
111111
}
112112

113+
override val duration: Double
114+
get() =
115+
runBlocking {
116+
if (hasMedia) getDurationSafely() else 0.0
117+
}
118+
113119
// Non-blocking aspect ratio property
114120
private val _aspectRatio = mutableStateOf(16f / 9f)
115121
override val aspectRatio: Float get() = _aspectRatio.value

mediaplayer/src/jvmMain/kotlin/io/github/kdroidfilter/composemediaplayer/windows/WindowsVideoPlayerState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ class WindowsVideoPlayerState : VideoPlayerState {
223223
override val positionText: String get() = formatTime(_currentTime)
224224
override val durationText: String get() = formatTime(_duration)
225225
override val currentTime: Double get() = _currentTime
226+
override val duration: Double get() = _duration
226227
private var errorMessage: String? by mutableStateOf(null)
227228

228229
// Fullscreen state

mediaplayer/src/webMain/kotlin/io/github/kdroidfilter/composemediaplayer/VideoPlayerState.web.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ open class DefaultVideoPlayerState : VideoPlayerState {
126126
// Current time of the media in seconds
127127
private var _currentTime: Double = 0.0
128128
override val currentTime: Double get() = _currentTime
129+
override val duration: Double get() = _currentDuration.toDouble()
129130

130131
// Job for handling seek operations
131132
internal var seekJob: Job? = null

0 commit comments

Comments
 (0)