Replace media3 DefaultTimeBar with custom implementation

This commit is contained in:
vfsfitvnm 2022-06-08 20:21:04 +02:00
parent 7f141fddf4
commit bb4d43d8a1
3 changed files with 24 additions and 39 deletions

View file

@ -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)

View file

@ -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<Long?>(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
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
}
override fun onScrubStop(
timeBar: TimeBar,
position: Long,
canceled: Boolean
) {
if (!canceled) {
scrubbingPosition = position
player.mediaController.seekTo(position)
},
onDragEnd = {
player.mediaController.seekTo(scrubbingPosition ?: player.mediaController.currentPosition)
player.currentPosition = player.mediaController.currentPosition
}
scrubbingPosition = null
}
})
}
},
update = {
it.setDuration(player.duration)
it.setPosition(player.currentPosition)
},
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()
)

View file

@ -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")