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,43 +301,45 @@ fun GetVideoController(
nostrUriCallback, nostrUriCallback,
context context
) { ) {
// REQUIRED TO BE RUN IN THE MAIN THREAD scope.launch(Dispatchers.Main) {
// REQUIRED TO BE RUN IN THE MAIN THREAD
// checks again because of race conditions. // checks again because of race conditions.
if (controller.value == null) { // still prone to race conditions. if (controller.value == null) { // still prone to race conditions.
controller.value = it controller.value = it
if (!it.isPlaying) { if (!it.isPlaying) {
if (keepPlayingMutex?.isPlaying == true) { if (keepPlayingMutex?.isPlaying == true) {
// There is a video playing, start this one on mute.
controller.value?.volume = 0f
} else {
// There is no other video playing. Use the default mute state to
// decide if sound is on or not.
controller.value?.volume = if (defaultToStart) 0f else 1f
}
}
controller.value?.setMediaItem(mediaItem.value)
controller.value?.prepare()
} else if (controller.value != it) {
// discards the new controller because there is an existing one
it.stop()
it.release()
controller.value?.let {
if (it.playbackState == Player.STATE_IDLE || it.playbackState == Player.STATE_ENDED) {
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 controller.value?.volume = 0f
} 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.
it.volume = if (defaultToStart) 0f else 1f controller.value?.volume = if (defaultToStart) 0f else 1f
} }
}
it.setMediaItem(mediaItem.value) controller.value?.setMediaItem(mediaItem.value)
it.prepare() controller.value?.prepare()
} else if (controller.value != it) {
// discards the new controller because there is an existing one
it.stop()
it.release()
controller.value?.let {
if (it.playbackState == Player.STATE_IDLE || it.playbackState == Player.STATE_ENDED) {
if (it.isPlaying) {
// There is a video playing, start this one on mute.
it.volume = 0f
} else {
// 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()
}
} }
} }
} }
@@ -345,18 +347,20 @@ fun GetVideoController(
} }
} else { } else {
controller.value?.let { controller.value?.let {
if (it.playbackState == Player.STATE_IDLE || it.playbackState == Player.STATE_ENDED) { scope.launch(Dispatchers.Main) {
if (it.isPlaying) { if (it.playbackState == Player.STATE_IDLE || it.playbackState == Player.STATE_ENDED) {
// There is a video playing, start this one on mute. if (it.isPlaying) {
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.setMediaItem(mediaItem.value)
it.prepare() it.prepare()
}
} }
} }
} }
@@ -386,29 +390,32 @@ fun GetVideoController(
nostrUriCallback, nostrUriCallback,
context context
) { ) {
// REQUIRED TO BE RUN IN THE MAIN THREAD scope.launch(Dispatchers.Main) {
// 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.
if (controller.value == null) { if (controller.value == null) {
controller.value = it controller.value = it
if (!it.isPlaying) { if (!it.isPlaying) {
if (keepPlayingMutex?.isPlaying == true) { if (keepPlayingMutex?.isPlaying == true) {
// There is a video playing, start this one on mute. // There is a video playing, start this one on mute.
controller.value?.volume = 0f controller.value?.volume = 0f
} 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
}
} }
}
controller.value?.setMediaItem(mediaItem.value) controller.value?.setMediaItem(mediaItem.value)
controller.value?.prepare() controller.value?.prepare()
} else if (controller.value != it) { } else if (controller.value != it) {
// discards the new controller because there is an existing one // discards the new controller because there is an existing one
it.stop() it.stop()
it.release() it.release()
}
} }
} }
} }