Fixes video release coroutine being killed by Android

This commit is contained in:
Vitor Pamplona
2023-11-29 10:17:57 -05:00
parent 44b25b7c7c
commit dbbfaae02c
2 changed files with 9 additions and 5 deletions
@@ -201,9 +201,8 @@ class Relay(
fun processNewRelayMessage(newMessage: String) { fun processNewRelayMessage(newMessage: String) {
val msgArray = Event.mapper.readTree(newMessage) val msgArray = Event.mapper.readTree(newMessage)
val type = msgArray.get(0).asText()
when (type) { when (val type = msgArray.get(0).asText()) {
"EVENT" -> { "EVENT" -> {
val subscriptionId = msgArray.get(1).asText() val subscriptionId = msgArray.get(1).asText()
val event = Event.fromJson(msgArray.get(2)) val event = Event.fromJson(msgArray.get(2))
@@ -80,6 +80,7 @@ import com.vitorpamplona.amethyst.ui.theme.VolumeBottomIconSize
import com.vitorpamplona.amethyst.ui.theme.imageModifier import com.vitorpamplona.amethyst.ui.theme.imageModifier
import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.ImmutableList
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flow
@@ -317,6 +318,7 @@ fun GetVideoController(
controller.value = MediaControllerState.Loading controller.value = MediaControllerState.Loading
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
Log.d("PlaybackService", "Preparing Video $videoUri ")
PlaybackClientController.prepareController( PlaybackClientController.prepareController(
uid, uid,
videoUri, videoUri,
@@ -367,12 +369,13 @@ fun GetVideoController(
} }
onDispose { onDispose {
scope.launch(Dispatchers.Main) { GlobalScope.launch(Dispatchers.Main) {
if (!keepPlaying.value) { if (!keepPlaying.value) {
// Stops and releases the media. // Stops and releases the media.
(controller.value as? MediaControllerState.Loaded)?.instance?.let { (controller.value as? MediaControllerState.Loaded)?.instance?.let {
it.stop() it.stop()
it.release() it.release()
Log.d("PlaybackService", "Releasing Video $videoUri ")
controller.value = MediaControllerState.NotStarted controller.value = MediaControllerState.NotStarted
} }
} }
@@ -391,6 +394,8 @@ fun GetVideoController(
if (controller.value == MediaControllerState.NotStarted) { if (controller.value == MediaControllerState.NotStarted) {
controller.value = MediaControllerState.Loading controller.value = MediaControllerState.Loading
Log.d("PlaybackService", "Preparing Video from Resume $videoUri ")
PlaybackClientController.prepareController( PlaybackClientController.prepareController(
uid, uid,
videoUri, videoUri,
@@ -425,11 +430,11 @@ fun GetVideoController(
} }
} }
if (event == Lifecycle.Event.ON_PAUSE) { if (event == Lifecycle.Event.ON_PAUSE) {
Log.d("PlaybackService", "Opening From Client - onPause ${controller.value} ") GlobalScope.launch(Dispatchers.Main) {
scope.launch(Dispatchers.Main) {
if (!keepPlaying.value) { if (!keepPlaying.value) {
// Stops and releases the media. // Stops and releases the media.
(controller.value as? MediaControllerState.Loaded)?.instance?.let { (controller.value as? MediaControllerState.Loaded)?.instance?.let {
Log.d("PlaybackService", "Releasing Video from Pause $videoUri ")
it.stop() it.stop()
it.release() it.release()
controller.value = MediaControllerState.NotStarted controller.value = MediaControllerState.NotStarted