mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-26 18:06:21 +02:00
Reverting to have videos starting from the IO thread.
This commit is contained in:
@@ -32,6 +32,7 @@ import androidx.compose.runtime.getValue
|
|||||||
import androidx.compose.runtime.mutableIntStateOf
|
import androidx.compose.runtime.mutableIntStateOf
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
@@ -281,70 +282,74 @@ 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.
|
||||||
DisposableEffect(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) {
|
scope.launch(Dispatchers.IO) {
|
||||||
PlaybackClientController.prepareController(
|
if (controller.value == null) {
|
||||||
uid,
|
PlaybackClientController.prepareController(
|
||||||
videoUri,
|
uid,
|
||||||
nostrUriCallback,
|
videoUri,
|
||||||
context
|
nostrUriCallback,
|
||||||
) {
|
context
|
||||||
// checks again because of race conditions.
|
) {
|
||||||
if (controller.value == null) { // still prone to race conditions.
|
// checks again because of race conditions.
|
||||||
controller.value = it
|
if (controller.value == null) { // still prone to race conditions.
|
||||||
|
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)
|
|
||||||
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)
|
controller.value?.setMediaItem(mediaItem)
|
||||||
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)
|
||||||
|
it.prepare()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
controller.value?.let {
|
||||||
controller.value?.let {
|
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.
|
it.volume = 0f
|
||||||
it.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
|
||||||
it.volume = if (defaultToStart) 0f else 1f
|
}
|
||||||
}
|
|
||||||
|
|
||||||
it.setMediaItem(mediaItem)
|
it.setMediaItem(mediaItem)
|
||||||
it.prepare()
|
it.prepare()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -366,34 +371,36 @@ fun GetVideoController(
|
|||||||
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
|
||||||
// if the controller is not null, just continue playing what the controller was playing
|
// if the controller is not null, just continue playing what the controller was playing
|
||||||
if (controller.value == null) {
|
scope.launch(Dispatchers.IO) {
|
||||||
PlaybackClientController.prepareController(
|
if (controller.value == null) {
|
||||||
uid,
|
PlaybackClientController.prepareController(
|
||||||
videoUri,
|
uid,
|
||||||
nostrUriCallback,
|
videoUri,
|
||||||
context
|
nostrUriCallback,
|
||||||
) {
|
context
|
||||||
// checks again to make sure no other thread has created a controller.
|
) {
|
||||||
if (controller.value == null) {
|
// checks again to make sure no other thread has created a controller.
|
||||||
controller.value = it
|
if (controller.value == null) {
|
||||||
|
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)
|
controller.value?.setMediaItem(mediaItem)
|
||||||
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -519,7 +526,8 @@ private fun RenderVideoPlayer(
|
|||||||
.defaultMinSize(minHeight = 100.dp)
|
.defaultMinSize(minHeight = 100.dp)
|
||||||
.align(Alignment.Center)
|
.align(Alignment.Center)
|
||||||
} else {
|
} else {
|
||||||
Modifier.fillMaxWidth()
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
.defaultMinSize(minHeight = 100.dp)
|
.defaultMinSize(minHeight = 100.dp)
|
||||||
.align(Alignment.Center)
|
.align(Alignment.Center)
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user