From 7906a94ca0beb68ec963d8a11e57ec3d4afa3368 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 7 Jun 2024 11:42:15 -0400 Subject: [PATCH] Moves the video release to a thread without damaging ON_PAUSE, ON_RELEASE order. --- .../amethyst/ui/components/VideoView.kt | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/VideoView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/VideoView.kt index 3d6c7a33e..3dd23ce83 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/VideoView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/VideoView.kt @@ -568,11 +568,20 @@ fun GetVideoController( if (event == Lifecycle.Event.ON_PAUSE) { if (!keepPlaying.value) { // Stops and releases the media. - controller.value?.let { - Log.d("PlaybackService", "Releasing Video from Pause $videoUri ") - it.stop() - it.release() - controller.value = null + // Makes sure the variable is cleared before the task is launched + // to avoid the ON_RELEASE running before ON_PAUSE's coroutine + val toRelease = controller.value + controller.value = null + + toRelease?.let { + it.pause() + + scope.launch(Dispatchers.Main) { + Log.d("PlaybackService", "Releasing Video from Pause $videoUri ") + it.stop() + it.release() + Log.d("PlaybackService", "Released Video from Pause $videoUri ") + } } } }