From bb4d43d8a1dfd2984d2671b26ee4006e1c5f9677 Mon Sep 17 00:00:00 2001 From: vfsfitvnm Date: Wed, 8 Jun 2022 20:21:04 +0200 Subject: [PATCH] Replace media3 DefaultTimeBar with custom implementation --- app/build.gradle.kts | 3 +- .../vfsfitvnm/vimusic/ui/views/PlayerView.kt | 58 +++++++------------ settings.gradle.kts | 2 - 3 files changed, 24 insertions(+), 39 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index d96aadf..c4ee7ea 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -85,7 +85,8 @@ dependencies { implementation(libs.accompanist.systemuicontroller) implementation(libs.android.media) - implementation(libs.bundles.media3) + implementation(libs.media3.session) + implementation(libs.media3.exoplayer) implementation(libs.datastore) diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/views/PlayerView.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/views/PlayerView.kt index c9dec75..3c7f802 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/views/PlayerView.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/views/PlayerView.kt @@ -6,6 +6,7 @@ import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* +import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.BasicText import androidx.compose.runtime.* import androidx.compose.ui.Alignment @@ -15,19 +16,15 @@ import androidx.compose.ui.draw.drawWithCache import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.ColorFilter import androidx.compose.ui.graphics.graphicsLayer -import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp -import androidx.compose.ui.viewinterop.AndroidView import androidx.media3.common.C import androidx.media3.common.MediaItem import androidx.media3.common.Player -import androidx.media3.ui.DefaultTimeBar -import androidx.media3.ui.TimeBar import coil.compose.AsyncImage import it.vfsfitvnm.vimusic.Database import it.vfsfitvnm.vimusic.R @@ -179,7 +176,7 @@ fun PlayerView( .padding(bottom = 72.dp) .fillMaxSize() ) { - var scrubbingPosition by remember { + var scrubbingPosition by remember(player.mediaItemIndex) { mutableStateOf(null) } @@ -272,7 +269,6 @@ fun PlayerView( .padding(horizontal = 32.dp) ) - BasicText( text = player.mediaMetadata.extras?.getStringArrayList("artistNames") ?.joinToString("") ?: "", @@ -283,40 +279,30 @@ fun PlayerView( .padding(horizontal = 32.dp) ) - AndroidView( - factory = { context -> - DefaultTimeBar(context).also { - it.setPlayedColor(colorPalette.text.toArgb()) - it.setUnplayedColor(colorPalette.textDisabled.toArgb()) - it.setScrubberColor(colorPalette.text.toArgb()) - it.addListener(object : TimeBar.OnScrubListener { - override fun onScrubStart(timeBar: TimeBar, position: Long) = Unit - - override fun onScrubMove(timeBar: TimeBar, position: Long) { - scrubbingPosition = position - } - - override fun onScrubStop( - timeBar: TimeBar, - position: Long, - canceled: Boolean - ) { - if (!canceled) { - scrubbingPosition = position - player.mediaController.seekTo(position) - player.currentPosition = player.mediaController.currentPosition - } - scrubbingPosition = null - } - }) + SeekBar( + value = scrubbingPosition ?: player.currentPosition, + minimumValue = 0, + maximumValue = player.duration, + onDragStart = { + scrubbingPosition = it + }, + onDrag = { delta -> + scrubbingPosition = if (player.duration != C.TIME_UNSET) { + scrubbingPosition?.plus(delta)?.coerceIn(0, player.duration) + } else { + null } }, - update = { - it.setDuration(player.duration) - it.setPosition(player.currentPosition) + onDragEnd = { + player.mediaController.seekTo(scrubbingPosition ?: player.mediaController.currentPosition) + player.currentPosition = player.mediaController.currentPosition + scrubbingPosition = null }, + color = colorPalette.text, + backgroundColor = colorPalette.textDisabled, + shape = RoundedCornerShape(8.dp), modifier = Modifier - .padding(top = 16.dp) + .padding(top = 24.dp, bottom = 12.dp) .padding(horizontal = 32.dp) .fillMaxWidth() ) diff --git a/settings.gradle.kts b/settings.gradle.kts index 018cb45..a5537a7 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -38,10 +38,8 @@ dependencyResolutionManagement { alias("room-compiler").to("androidx.room", "room-compiler").versionRef("room") version("media3", "1.0.0-alpha03") - alias("media3-ui").to("androidx.media3", "media3-ui").versionRef("media3") alias("media3-session").to("androidx.media3", "media3-session").versionRef("media3") alias("media3-exoplayer").to("androidx.media3", "media3-exoplayer").versionRef("media3") - bundle("media3", listOf("media3-ui", "media3-session", "media3-exoplayer")) version("ktor", "2.0.2") alias("ktor-client-core").to("io.ktor", "ktor-client-core").versionRef("ktor")