-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.kt
49 lines (44 loc) · 1.2 KB
/
Player.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.player.extension
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
/**
* Get a snapshot of the current media items
*/
fun Player.getCurrentMediaItems(): List<MediaItem> {
val count = mediaItemCount
if (count == 0) {
return emptyList()
}
return buildList(count) {
repeat(count) { i ->
add(getMediaItemAt(i))
}
}
}
/**
* Get playback speed
*
* @return [Player.getPlaybackParameters] speed
*/
fun Player.getPlaybackSpeed(): Float {
return playbackParameters.speed
}
/**
* Current position percent
*
* @return the current position in percent [0,1].
*/
fun Player.currentPositionPercentage(): Float {
return currentPosition / duration.coerceAtLeast(1).toFloat()
}
/**
* Handle audio focus with the currently set [AudioAttributes][androidx.media3.common.AudioAttributes].
* @param handleAudioFocus `true` if the player should handle audio focus, `false` otherwise.
*/
fun Player.setHandleAudioFocus(handleAudioFocus: Boolean) {
setAudioAttributes(audioAttributes, handleAudioFocus)
}