Fixes video keep playing bug when pressing the bottom bar twice.

This commit is contained in:
Vitor Pamplona
2023-09-21 09:09:24 -04:00
parent 02786fb7d3
commit 932abdd412

View File

@@ -282,12 +282,14 @@ fun GetVideoController(
UUID.randomUUID().toString() UUID.randomUUID().toString()
} }
val scope = rememberCoroutineScope()
// Prepares a VideoPlayer from the foreground service. // Prepares a VideoPlayer from the foreground service.
LaunchedEffect(key1 = videoUri) { DisposableEffect(key1 = videoUri) {
// 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) {
launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
PlaybackClientController.prepareController( PlaybackClientController.prepareController(
uid, uid,
videoUri, videoUri,
@@ -351,12 +353,20 @@ fun GetVideoController(
} }
} }
} }
onDispose {
if (!keepPlaying.value) {
// Stops and releases the media.
controller.value?.stop()
controller.value?.release()
controller.value = null
}
}
} }
// User pauses and resumes the app. What to do with videos? // User pauses and resumes the app. What to do with videos?
val scope = rememberCoroutineScope()
val lifeCycleOwner = LocalLifecycleOwner.current val lifeCycleOwner = LocalLifecycleOwner.current
DisposableEffect(key1 = videoUri) { DisposableEffect(key1 = lifeCycleOwner) {
val observer = LifecycleEventObserver { _, event -> val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_RESUME) { if (event == Lifecycle.Event.ON_RESUME) {
// if the controller is null, restarts the controller with a new one // if the controller is null, restarts the controller with a new one
@@ -364,7 +374,7 @@ fun GetVideoController(
if (controller.value == null) { if (controller.value == null) {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
PlaybackClientController.prepareController( PlaybackClientController.prepareController(
UUID.randomUUID().toString(), uid,
videoUri, videoUri,
nostrUriCallback, nostrUriCallback,
context context
@@ -408,13 +418,6 @@ fun GetVideoController(
lifeCycleOwner.lifecycle.addObserver(observer) lifeCycleOwner.lifecycle.addObserver(observer)
onDispose { onDispose {
lifeCycleOwner.lifecycle.removeObserver(observer) lifeCycleOwner.lifecycle.removeObserver(observer)
if (!keepPlaying.value) {
// Stops and releases the media.
controller.value?.stop()
controller.value?.release()
controller.value = null
}
} }
} }