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.flow.flow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.util.UUID import java.util.UUID
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.math.abs import kotlin.math.abs
public val DEFAULT_MUTED_SETTING = mutableStateOf(true) public val DEFAULT_MUTED_SETTING = mutableStateOf(true)
@ -394,6 +395,8 @@ fun GetVideoController(
) { ) {
val context = LocalContext.current val context = LocalContext.current
val onlyOnePreparing = AtomicBoolean()
val controller = val controller =
remember(videoUri) { remember(videoUri) {
mutableStateOf( mutableStateOf(
@ -421,31 +424,36 @@ fun GetVideoController(
// If it is not null, the user might have come back from a playing video, like clicking on // If it is not null, the user might have come back from a playing video, like clicking on
// the notification of the video player. // the notification of the video player.
if (controller.value == null) { if (controller.value == null) {
scope.launch(Dispatchers.IO) { // If there is a connection, don't wait.
Log.d("PlaybackService", "Preparing Video $videoUri ") if (!onlyOnePreparing.getAndSet(true)) {
PlaybackClientController.prepareController( scope.launch(Dispatchers.IO) {
uid, Log.d("PlaybackService", "Preparing Video $videoUri ")
videoUri, PlaybackClientController.prepareController(
nostrUriCallback, uid,
context, videoUri,
) { nostrUriCallback,
scope.launch(Dispatchers.Main) { context,
// REQUIRED TO BE RUN IN THE MAIN THREAD ) {
if (!it.isPlaying) { scope.launch(Dispatchers.Main) {
if (keepPlayingMutex?.isPlaying == true) { // REQUIRED TO BE RUN IN THE MAIN THREAD
// There is a video playing, start this one on mute. if (!it.isPlaying) {
it.volume = 0f if (keepPlayingMutex?.isPlaying == true) {
} else { // There is a video playing, start this one on mute.
// There is no other video playing. Use the default mute state to it.volume = 0f
// decide if sound is on or not. } else {
it.volume = if (defaultToStart) 0f else 1f // There is no other video playing. Use the default mute state to
// decide if sound is on or not.
it.volume = if (defaultToStart) 0f else 1f
}
} }
it.setMediaItem(mediaItem.value)
it.prepare()
controller.value = it
onlyOnePreparing.getAndSet(false)
} }
it.setMediaItem(mediaItem.value)
it.prepare()
controller.value = it
} }
} }
} }
@ -454,6 +462,8 @@ fun GetVideoController(
controller.value?.let { controller.value?.let {
scope.launch(Dispatchers.Main) { scope.launch(Dispatchers.Main) {
if (it.playbackState == Player.STATE_IDLE || it.playbackState == Player.STATE_ENDED) { if (it.playbackState == Player.STATE_IDLE || it.playbackState == Player.STATE_ENDED) {
Log.d("PlaybackService", "Preparing Existing Video $videoUri ")
if (it.isPlaying) { if (it.isPlaying) {
// There is a video playing, start this one on mute. // There is a video playing, start this one on mute.
it.volume = 0f it.volume = 0f
@ -497,32 +507,35 @@ fun GetVideoController(
// if the controller is null, restarts the controller with a new one // 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 the controller is not null, just continue playing what the controller was playing
if (controller.value == null) { if (controller.value == null) {
scope.launch(Dispatchers.IO) { if (!onlyOnePreparing.getAndSet(true)) {
Log.d("PlaybackService", "Preparing Video from Resume $videoUri ") scope.launch(Dispatchers.IO) {
PlaybackClientController.prepareController( Log.d("PlaybackService", "Preparing Video from Resume $videoUri ")
uid, PlaybackClientController.prepareController(
videoUri, uid,
nostrUriCallback, videoUri,
context, nostrUriCallback,
) { context,
scope.launch(Dispatchers.Main) { ) {
// REQUIRED TO BE RUN IN THE MAIN THREAD scope.launch(Dispatchers.Main) {
// checks again to make sure no other thread has created a controller. // REQUIRED TO BE RUN IN THE MAIN THREAD
if (!it.isPlaying) { // checks again to make sure no other thread has created a controller.
if (keepPlayingMutex?.isPlaying == true) { if (!it.isPlaying) {
// There is a video playing, start this one on mute. if (keepPlayingMutex?.isPlaying == true) {
it.volume = 0f // There is a video playing, start this one on mute.
} else { it.volume = 0f
// There is no other video playing. Use the default mute state to } else {
// decide if sound is on or not. // There is no other video playing. Use the default mute state to
it.volume = if (defaultToStart) 0f else 1f // decide if sound is on or not.
it.volume = if (defaultToStart) 0f else 1f
}
} }
it.setMediaItem(mediaItem.value)
it.prepare()
controller.value = it
onlyOnePreparing.getAndSet(false)
} }
it.setMediaItem(mediaItem.value)
it.prepare()
controller.value = it
} }
} }
} }