Makes sure only one pepare is run for each video view.

pull/842/head
Vitor Pamplona 2024-04-18 14:46:01 -04:00
rodzic ef363457e8
commit 8cf04967c3
1 zmienionych plików z 60 dodań i 47 usunięć

Wyświetl plik

@ -115,6 +115,7 @@ import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import java.util.UUID
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.math.abs
public val DEFAULT_MUTED_SETTING = mutableStateOf(true)
@ -394,6 +395,8 @@ fun GetVideoController(
) {
val context = LocalContext.current
val onlyOnePreparing = AtomicBoolean()
val controller =
remember(videoUri) {
mutableStateOf(
@ -421,6 +424,8 @@ fun GetVideoController(
// If it is not null, the user might have come back from a playing video, like clicking on
// the notification of the video player.
if (controller.value == null) {
// If there is a connection, don't wait.
if (!onlyOnePreparing.getAndSet(true)) {
scope.launch(Dispatchers.IO) {
Log.d("PlaybackService", "Preparing Video $videoUri ")
PlaybackClientController.prepareController(
@ -446,6 +451,9 @@ fun GetVideoController(
it.prepare()
controller.value = it
onlyOnePreparing.getAndSet(false)
}
}
}
}
@ -454,6 +462,8 @@ fun GetVideoController(
controller.value?.let {
scope.launch(Dispatchers.Main) {
if (it.playbackState == Player.STATE_IDLE || it.playbackState == Player.STATE_ENDED) {
Log.d("PlaybackService", "Preparing Existing Video $videoUri ")
if (it.isPlaying) {
// There is a video playing, start this one on mute.
it.volume = 0f
@ -497,6 +507,7 @@ fun GetVideoController(
// if the controller is null, restarts the controller with a new one
// if the controller is not null, just continue playing what the controller was playing
if (controller.value == null) {
if (!onlyOnePreparing.getAndSet(true)) {
scope.launch(Dispatchers.IO) {
Log.d("PlaybackService", "Preparing Video from Resume $videoUri ")
PlaybackClientController.prepareController(
@ -523,6 +534,8 @@ fun GetVideoController(
it.prepare()
controller.value = it
onlyOnePreparing.getAndSet(false)
}
}
}
}