Skip to content

Commit

Permalink
fix: Player not opening files with characters like "#" in its name
Browse files Browse the repository at this point in the history
fix: Player UI not updating song duration

Signed-off-by: Gabriel Fontán <[email protected]>
  • Loading branch information
BobbyESP committed Apr 29, 2024
1 parent 806de1c commit 26591f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fun ArtworkAsyncImage(
state !is AsyncImagePainter.State.Error && state !is AsyncImagePainter.State.Empty
},
contentDescription = "Song cover",
contentScale = ContentScale.Fit,
contentScale = ContentScale.Crop,
isPreview = false
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
Expand Down Expand Up @@ -206,17 +207,19 @@ private fun PlayerControls(
val playerState = viewState.uiState

val playingSong =
viewModel.playingSong.collectAsStateWithLifecycle().value?.mediaMetadata ?: return
viewModel.playingSong.collectAsStateWithLifecycle().value?.mediaMetadata

val readyState = playerState as? MediaplayerViewModel.PlayerState.Ready
val progress = readyState?.progress ?: return
val progress = readyState?.progress ?: 0f

var sliderPosition by remember {
mutableStateOf<Float?>(null)
}

val duration by remember(readyState.duration) {
mutableLongStateOf(readyState.duration)
val duration by remember(readyState?.duration) {
derivedStateOf {
mutableLongStateOf(readyState?.duration ?: 0L)
}
}

var temporalProgressString by remember {
Expand All @@ -231,9 +234,10 @@ private fun PlayerControls(
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
ArtworkAsyncImage(
artworkPath = playingSong.artworkUri,
artworkPath = playingSong?.artworkUri,
modifier = imageModifier
.fillMaxWidth()
.aspectRatio(1f)
.padding(horizontal = 24.dp, vertical = 16.dp)
.clip(MaterialTheme.shapes.small)
)
Expand All @@ -242,11 +246,11 @@ private fun PlayerControls(

Column(modifier = Modifier.padding(horizontal = 24.dp)) {
Text(
text = playingSong.title.toString(),
text = playingSong?.title.toString(),
style = MaterialTheme.typography.titleLarge.copy(fontWeight = FontWeight.Medium)
)
MarqueeText(
text = playingSong.artist.toString(),
text = playingSong?.artist.toString(),
style = MaterialTheme.typography.bodyLarge,
customEasing = EaseInOutSine,
sideGradient = MarqueeTextGradientOptions(
Expand All @@ -263,9 +267,9 @@ private fun PlayerControls(
MutableInteractionSource()
}

val songDuration by remember(readyState.duration) {
val songDuration by remember(readyState?.duration) {
derivedStateOf {
formatDuration(readyState.duration)
formatDuration(readyState?.duration ?: 0L)
}
}

Expand All @@ -278,7 +282,7 @@ private fun PlayerControls(
value = sliderPosition ?: progress,
onValueChange = {
sliderPosition = it
temporalProgressString = formatDuration((it * duration).toLong())
temporalProgressString = formatDuration((it * duration.longValue).toLong())
},
onValueChangeFinished = {
viewModel.seekTo(sliderPosition ?: return@Slider)
Expand Down Expand Up @@ -308,7 +312,7 @@ private fun PlayerControls(

Row(modifier = Modifier.padding(horizontal = 2.dp)) {
Text(
text = temporalProgressString ?: readyState.progressString,
text = temporalProgressString ?: readyState?.progressString ?: "00:00",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bobbyesp.metadator.presentation.pages.mediaplayer

import android.content.Context
import android.net.Uri
import android.util.Log
import androidx.annotation.OptIn
import androidx.lifecycle.ViewModel
Expand All @@ -26,6 +27,7 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject

@OptIn(UnstableApi::class)
Expand Down Expand Up @@ -116,7 +118,7 @@ class MediaplayerViewModel @Inject constructor(

private fun loadSongInfo(song: Song) {
val mediaItem = MediaItem.Builder()
.setUri(song.path)
.setUri(Uri.fromFile(File(song.path)))
.setMediaMetadata(
MediaMetadata.Builder()
.setTitle(song.title)
Expand All @@ -134,7 +136,7 @@ class MediaplayerViewModel @Inject constructor(
private fun loadQueueSongs(songs: List<Song>) {
val mediaItems = songs.map { song ->
MediaItem.Builder()
.setUri(song.path)
.setUri(Uri.fromFile(File(song.path)))
.setMediaMetadata(
MediaMetadata.Builder()
.setTitle(song.title)
Expand Down

0 comments on commit 26591f6

Please sign in to comment.