Starting videos from Main, but in a thread.

This commit is contained in:
Vitor Pamplona
2023-10-30 17:31:33 -04:00
parent 17130c81c8
commit 6b7ec2caa9
@@ -301,6 +301,7 @@ fun GetVideoController(
nostrUriCallback, nostrUriCallback,
context context
) { ) {
scope.launch(Dispatchers.Main) {
// REQUIRED TO BE RUN IN THE MAIN THREAD // REQUIRED TO BE RUN IN THE MAIN THREAD
// checks again because of race conditions. // checks again because of race conditions.
@@ -343,8 +344,10 @@ fun GetVideoController(
} }
} }
} }
}
} else { } else {
controller.value?.let { controller.value?.let {
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) {
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.
@@ -360,6 +363,7 @@ fun GetVideoController(
} }
} }
} }
}
onDispose { onDispose {
if (!keepPlaying.value) { if (!keepPlaying.value) {
@@ -386,6 +390,7 @@ fun GetVideoController(
nostrUriCallback, nostrUriCallback,
context context
) { ) {
scope.launch(Dispatchers.Main) {
// REQUIRED TO BE RUN IN THE MAIN THREAD // REQUIRED TO BE RUN IN THE MAIN THREAD
// checks again to make sure no other thread has created a controller. // checks again to make sure no other thread has created a controller.
@@ -399,7 +404,8 @@ fun GetVideoController(
} else { } else {
// There is no other video playing. Use the default mute state to // There is no other video playing. Use the default mute state to
// decide if sound is on or not. // decide if sound is on or not.
controller.value?.volume = if (defaultToStart) 0f else 1f controller.value?.volume =
if (defaultToStart) 0f else 1f
} }
} }
@@ -414,6 +420,7 @@ fun GetVideoController(
} }
} }
} }
}
if (event == Lifecycle.Event.ON_PAUSE) { if (event == Lifecycle.Event.ON_PAUSE) {
if (!keepPlaying.value) { if (!keepPlaying.value) {
// Stops and releases the media. // Stops and releases the media.