-
Notifications
You must be signed in to change notification settings - Fork 1
/
ComposablePlayer.kt
250 lines (229 loc) · 6.99 KB
/
ComposablePlayer.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.ui.extension
import androidx.compose.runtime.Composable
import androidx.compose.runtime.FloatState
import androidx.compose.runtime.IntState
import androidx.compose.runtime.LongState
import androidx.compose.runtime.State
import androidx.compose.runtime.asFloatState
import androidx.compose.runtime.asIntState
import androidx.compose.runtime.asLongState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.common.Player.Commands
import androidx.media3.common.VideoSize
import ch.srgssr.pillarbox.player.DefaultUpdateInterval
import ch.srgssr.pillarbox.player.availableCommandsAsFlow
import ch.srgssr.pillarbox.player.currentBufferedPercentageAsFlow
import ch.srgssr.pillarbox.player.currentMediaMetadataAsFlow
import ch.srgssr.pillarbox.player.currentPositionAsFlow
import ch.srgssr.pillarbox.player.durationAsFlow
import ch.srgssr.pillarbox.player.extension.getCurrentMediaItems
import ch.srgssr.pillarbox.player.extension.getPlaybackSpeed
import ch.srgssr.pillarbox.player.getAspectRatioAsFlow
import ch.srgssr.pillarbox.player.getCurrentMediaItemIndexAsFlow
import ch.srgssr.pillarbox.player.getCurrentMediaItemsAsFlow
import ch.srgssr.pillarbox.player.getPlaybackSpeedAsFlow
import ch.srgssr.pillarbox.player.isCurrentMediaItemLiveAsFlow
import ch.srgssr.pillarbox.player.isPlayingAsFlow
import ch.srgssr.pillarbox.player.mediaItemCountAsFlow
import ch.srgssr.pillarbox.player.playWhenReadyAsFlow
import ch.srgssr.pillarbox.player.playbackStateAsFlow
import ch.srgssr.pillarbox.player.playerErrorAsFlow
import ch.srgssr.pillarbox.player.shuffleModeEnabledAsFlow
import ch.srgssr.pillarbox.player.videoSizeAsFlow
import kotlin.time.Duration
/**
* Composable helper function to facilitate compose integration
*/
/**
* Is playing [Player.isPlaying]
*/
@Composable
fun Player.isPlayingAsState(): State<Boolean> {
val flow = remember(this) {
isPlayingAsFlow()
}
return flow.collectAsState(initial = isPlaying)
}
/**
* Play when ready as state [Player.getPlayWhenReady]
*/
@Composable
fun Player.playWhenReadyAsState(): State<Boolean> {
val flow = remember(this) {
playWhenReadyAsFlow()
}
return flow.collectAsState(initial = playWhenReady)
}
/**
* Playback state [Player.getPlaybackState]
*/
@Composable
fun Player.playbackStateAsState(): IntState {
val flow = remember(this) {
playbackStateAsFlow()
}
return flow.collectAsState(initial = playbackState).asIntState()
}
/**
* Current position [Player.getCurrentPosition]
*/
@Composable
fun Player.currentPositionAsState(): LongState {
val flow = remember(this) {
currentPositionAsFlow()
}
return flow.collectAsState(initial = currentPosition).asLongState()
}
/**
* Current buffered percentage [Player.getBufferedPercentage]
*/
@Composable
fun Player.currentBufferedPercentageAsState(updateInterval: Duration = DefaultUpdateInterval): FloatState {
val flow = remember(this, updateInterval) {
currentBufferedPercentageAsFlow(updateInterval)
}
return flow.collectAsState(initial = bufferedPercentage / 100f).asFloatState()
}
/**
* Duration [Player.getDuration]
*/
@Composable
fun Player.durationAsState(): LongState {
val flow = remember(this) {
durationAsFlow()
}
return flow.collectAsState(initial = duration).asLongState()
}
/**
* Available commands [Player.getAvailableCommands]
*/
@Composable
fun Player.availableCommandsAsState(): State<Commands> {
val flow = remember(this) {
availableCommandsAsFlow()
}
return flow.collectAsState(initial = availableCommands)
}
/**
* Player error [Player.getPlayerError]
*/
@Composable
fun Player.playerErrorAsState(): State<PlaybackException?> {
val flow = remember(this) {
playerErrorAsFlow()
}
return flow.collectAsState(initial = playerError)
}
/**
* Shuffle mode enabled [Player.getShuffleModeEnabled]
*/
@Composable
fun Player.shuffleModeEnabledAsState(): State<Boolean> {
val flow = remember(this) {
shuffleModeEnabledAsFlow()
}
return flow.collectAsState(initial = shuffleModeEnabled)
}
/**
* Media item count [Player.getMediaItemCount]
*/
@Composable
fun Player.mediaItemCountAsState(): IntState {
val flow = remember(this) {
mediaItemCountAsFlow()
}
return flow.collectAsState(initial = mediaItemCount).asIntState()
}
/**
* @return true if [Player.getMediaItemCount] > 0
*/
@Composable
fun Player.hasMediaItemsAsState(): State<Boolean> {
val mediaItemCount by mediaItemCountAsState()
return remember {
derivedStateOf { mediaItemCount > 0 }
}
}
/**
* Playback speed [Player.getPlaybackParameters]
*/
@Composable
fun Player.playbackSpeedAsState(): FloatState {
val flow = remember(this) {
getPlaybackSpeedAsFlow()
}
return flow.collectAsState(initial = getPlaybackSpeed()).asFloatState()
}
/**
* Current media metadata [Player.getMediaMetadata]
*/
@Composable
fun Player.currentMediaMetadataAsState(): State<MediaMetadata> {
val flow = remember(this) {
currentMediaMetadataAsFlow()
}
return flow.collectAsState(initial = mediaMetadata)
}
/**
* Current media item index as state [Player.getCurrentMediaItem]
*/
@Composable
fun Player.currentMediaItemIndexAsState(): IntState {
val flow = remember(this) {
getCurrentMediaItemIndexAsFlow()
}
return flow.collectAsState(initial = currentMediaItemIndex).asIntState()
}
/**
* Get current media items as state [Player.getCurrentMediaItems]
*/
@Composable
fun Player.getCurrentMediaItemsAsState(): State<List<MediaItem>> {
val flow = remember(this) {
getCurrentMediaItemsAsFlow()
}
return flow.collectAsState(initial = getCurrentMediaItems())
}
/**
* Video size as state [Player.getVideoSize]
*/
@Composable
fun Player.videoSizeAsState(): State<VideoSize> {
val flow = remember(this) {
videoSizeAsFlow()
}
return flow.collectAsState(initial = videoSize)
}
/**
* Get aspect ratio of the current video as [State].
*
* @param defaultAspectRatio The aspect ratio when the video size is unknown, or for audio content.
*/
@Composable
fun Player.getAspectRatioAsState(defaultAspectRatio: Float): FloatState {
val flow = remember(this, defaultAspectRatio) {
getAspectRatioAsFlow(defaultAspectRatio = defaultAspectRatio)
}
return flow.collectAsState(initial = defaultAspectRatio).asFloatState()
}
/**
* @return `true` if current media item is a live stream.
*/
@Composable
fun Player.isCurrentMediaItemLiveAsState(): State<Boolean> {
val flow = remember(this) {
isCurrentMediaItemLiveAsFlow()
}
return flow.collectAsState(initial = isCurrentMediaItemLive)
}