Moves the creation of the MediaItem to a thread.

This commit is contained in:
Vitor Pamplona 2023-12-17 17:26:53 -05:00
parent 9026c877ac
commit 97c78aaa3c

View File

@ -37,6 +37,7 @@ import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue 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.produceState
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
@ -223,7 +224,12 @@ fun VideoView(
Box(modifier, contentAlignment = Alignment.Center) { Box(modifier, contentAlignment = Alignment.Center) {
if (!automaticallyStartPlayback.value) { if (!automaticallyStartPlayback.value) {
DisplayBlurHash(blurhash, null, ContentScale.Crop, MaterialTheme.colorScheme.imageModifier) DisplayBlurHash(
blurhash,
null,
ContentScale.Crop,
MaterialTheme.colorScheme.imageModifier
)
IconButton( IconButton(
modifier = Modifier.size(Size75dp), modifier = Modifier.size(Size75dp),
onClick = { automaticallyStartPlayback.value = true } onClick = { automaticallyStartPlayback.value = true }
@ -273,54 +279,73 @@ fun VideoViewInner(
onDialog: ((Boolean) -> Unit)? = null onDialog: ((Boolean) -> Unit)? = null
) { ) {
VideoPlayerActiveMutex(videoUri) { modifier, activeOnScreen -> VideoPlayerActiveMutex(videoUri) { modifier, activeOnScreen ->
val mediaItem = remember(videoUri) { GetMediaItem(videoUri, title, artworkUri, authorName) { mediaItem ->
mutableStateOf( GetVideoController(
MediaItem.Builder() mediaItem = mediaItem,
.setMediaId(videoUri) videoUri = videoUri,
.setUri(videoUri) defaultToStart = defaultToStart,
.setMediaMetadata( nostrUriCallback = nostrUriCallback
MediaMetadata.Builder() ) { controller, keepPlaying ->
.setArtist(authorName?.ifBlank { null }) RenderVideoPlayer(
.setTitle(title?.ifBlank { null } ?: videoUri) controller = controller,
.setArtworkUri( thumbData = thumb,
try { roundedCorner = roundedCorner,
if (artworkUri != null) { dimensions = dimensions,
Uri.parse(artworkUri) blurhash = blurhash,
} else { topPaddingForControllers = topPaddingForControllers,
null waveform = waveform,
} keepPlaying = keepPlaying,
} catch (e: Exception) { automaticallyStartPlayback = automaticallyStartPlayback,
null activeOnScreen = activeOnScreen,
} modifier = modifier,
) onControllerVisibilityChanged = onControllerVisibilityChanged,
.build() onDialog = onDialog
)
}
}
}
}
@Composable
fun GetMediaItem(
videoUri: String,
title: String?,
artworkUri: String?,
authorName: String?,
inner: @Composable (State<MediaItem>) -> Unit
) {
val mediaItem = produceState<MediaItem?>(
initialValue = null,
key1 = videoUri
) {
this.value = MediaItem.Builder()
.setMediaId(videoUri)
.setUri(videoUri)
.setMediaMetadata(
MediaMetadata.Builder()
.setArtist(authorName?.ifBlank { null })
.setTitle(title?.ifBlank { null } ?: videoUri)
.setArtworkUri(
try {
if (artworkUri != null) {
Uri.parse(artworkUri)
} else {
null
}
} catch (e: Exception) {
null
}
) )
.build() .build()
) )
} .build()
}
GetVideoController( mediaItem.value?.let {
mediaItem = mediaItem, val myState = remember(videoUri) {
videoUri = videoUri, mutableStateOf(it)
defaultToStart = defaultToStart,
nostrUriCallback = nostrUriCallback
) { controller, keepPlaying ->
RenderVideoPlayer(
controller = controller,
thumbData = thumb,
roundedCorner = roundedCorner,
dimensions = dimensions,
blurhash = blurhash,
topPaddingForControllers = topPaddingForControllers,
waveform = waveform,
keepPlaying = keepPlaying,
automaticallyStartPlayback = automaticallyStartPlayback,
activeOnScreen = activeOnScreen,
modifier = modifier,
onControllerVisibilityChanged = onControllerVisibilityChanged,
onDialog = onDialog
)
} }
inner(myState)
} }
} }
@ -339,7 +364,7 @@ sealed class MediaControllerState {
@Composable @Composable
@OptIn(UnstableApi::class) @OptIn(UnstableApi::class)
fun GetVideoController( fun GetVideoController(
mediaItem: MutableState<MediaItem>, mediaItem: State<MediaItem>,
videoUri: String, videoUri: String,
defaultToStart: Boolean = false, defaultToStart: Boolean = false,
nostrUriCallback: String? = null, nostrUriCallback: String? = null,