From 5a2daf1ab653c8f03f39c4705341651b8c80a076 Mon Sep 17 00:00:00 2001 From: nift4 Date: Tue, 10 Sep 2024 15:20:58 +0200 Subject: [PATCH] use durationMs from media3 --- .../gramophone/logic/utils/LastPlayedManager.kt | 6 ++---- .../gramophone/logic/utils/MediaStoreUtils.kt | 7 ++++--- .../gramophone/ui/components/FullBottomSheet.kt | 15 ++++++--------- .../ui/fragments/DetailDialogFragment.kt | 2 +- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/app/src/main/kotlin/org/akanework/gramophone/logic/utils/LastPlayedManager.kt b/app/src/main/kotlin/org/akanework/gramophone/logic/utils/LastPlayedManager.kt index dd3749149..42d101f03 100644 --- a/app/src/main/kotlin/org/akanework/gramophone/logic/utils/LastPlayedManager.kt +++ b/app/src/main/kotlin/org/akanework/gramophone/logic/utils/LastPlayedManager.kt @@ -116,7 +116,7 @@ class LastPlayedManager(context: Context, b.writeLong(it.mediaMetadata.extras?.getLong("GenreId")) b.writeStringSafe(it.mediaMetadata.extras?.getString("Author")) b.writeInt(it.mediaMetadata.extras?.getInt("CdTrackNumber")) - b.writeLong(it.mediaMetadata.extras?.getLong("Duration")) + b.writeLong(it.mediaMetadata.durationMs) b.writeStringUnsafe(it.mediaMetadata.extras?.getString("Path")) b.writeLong(it.mediaMetadata.extras?.getLong("ModifiedDate")) b.toString() @@ -219,6 +219,7 @@ class LastPlayedManager(context: Context, .setDiscNumber(discNumber) .setRecordingYear(recordingYear) .setReleaseYear(releaseYear) + .setDurationMs(duration) .setIsBrowsable(isBrowsable) .setIsPlayable(isPlayable) .setExtras(Bundle().apply { @@ -238,9 +239,6 @@ class LastPlayedManager(context: Context, putInt("CdTrackNumber", cdTrackNumber) } putString("Author", author) - if (duration != null) { - putLong("Duration", duration) - } putString("Path", path) if (modifiedDate != null) { putLong("ModifiedDate", modifiedDate) diff --git a/app/src/main/kotlin/org/akanework/gramophone/logic/utils/MediaStoreUtils.kt b/app/src/main/kotlin/org/akanework/gramophone/logic/utils/MediaStoreUtils.kt index ec8cb47d5..a5eb10b21 100644 --- a/app/src/main/kotlin/org/akanework/gramophone/logic/utils/MediaStoreUtils.kt +++ b/app/src/main/kotlin/org/akanework/gramophone/logic/utils/MediaStoreUtils.kt @@ -20,8 +20,10 @@ package org.akanework.gramophone.logic.utils import android.content.Context import android.os.Bundle import android.os.Parcelable +import androidx.annotation.OptIn import androidx.media3.common.MediaItem import androidx.media3.common.MediaMetadata +import androidx.media3.common.util.UnstableApi import androidx.preference.PreferenceManager import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -51,6 +53,7 @@ object MediaStoreUtils { * @param context * @return */ + @OptIn(UnstableApi::class) private fun getAllSongs(context: Context): ReaderResult { val prefs = PreferenceManager.getDefaultSharedPreferences(context) val limitValue = prefs.getInt( @@ -74,6 +77,7 @@ object MediaStoreUtils { .Builder() .setIsBrowsable(false) .setIsPlayable(true) + .setDurationMs(duration) .setTitle(title) .setWriter(writer) .setCompilation(compilation) @@ -103,9 +107,6 @@ object MediaStoreUtils { if (addDate != null) { putLong("AddDate", addDate) } - if (duration != null) { - putLong("Duration", duration) - } if (modifiedDate != null) { putLong("ModifiedDate", modifiedDate) } diff --git a/app/src/main/kotlin/org/akanework/gramophone/ui/components/FullBottomSheet.kt b/app/src/main/kotlin/org/akanework/gramophone/ui/components/FullBottomSheet.kt index cf27f6f62..2dcec1260 100644 --- a/app/src/main/kotlin/org/akanework/gramophone/ui/components/FullBottomSheet.kt +++ b/app/src/main/kotlin/org/akanework/gramophone/ui/components/FullBottomSheet.kt @@ -142,7 +142,7 @@ class FullBottomSheet private val touchListener = object : SeekBar.OnSeekBarChangeListener, Slider.OnSliderTouchListener { override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { if (fromUser) { - val dest = instance?.currentMediaItem?.mediaMetadata?.extras?.getLong("Duration") + val dest = instance?.currentMediaItem?.mediaMetadata?.durationMs if (dest != null) { bottomSheetFullPosition.text = CalculationUtils.convertDurationToTimeStamp((progress.toLong())) @@ -470,7 +470,7 @@ class FullBottomSheet bottomSheetFullSlider.addOnChangeListener { _, value, isUser -> if (isUser) { - val dest = instance?.currentMediaItem?.mediaMetadata?.extras?.getLong("Duration") + val dest = instance?.currentMediaItem?.mediaMetadata?.durationMs if (dest != null) { bottomSheetFullPosition.text = CalculationUtils.convertDurationToTimeStamp((value).toLong()) @@ -924,8 +924,7 @@ class FullBottomSheet bottomSheetFullSubtitle.setTextAnimation( mediaItem?.mediaMetadata?.artist ?: context.getString(R.string.unknown_artist), skipAnimation = firstTime ) - bottomSheetFullDuration.text = - mediaItem?.mediaMetadata?.extras?.getLong("Duration") + bottomSheetFullDuration.text = mediaItem?.mediaMetadata?.durationMs ?.let { CalculationUtils.convertDurationToTimeStamp(it) } if (playlistNowPlaying != null) { playlistNowPlaying!!.text = mediaItem?.mediaMetadata?.title @@ -952,7 +951,7 @@ class FullBottomSheet playlistNowPlayingCover?.dispose() } val position = CalculationUtils.convertDurationToTimeStamp(instance?.currentPosition ?: 0) - val duration = instance?.currentMediaItem?.mediaMetadata?.extras?.getLong("Duration") + val duration = instance?.currentMediaItem?.mediaMetadata?.durationMs if (duration != null && !isUserTracking) { bottomSheetFullSeekBar.max = duration.toInt() bottomSheetFullSeekBar.progress = instance?.currentPosition?.toInt() ?: 0 @@ -1286,9 +1285,7 @@ class FullBottomSheet holder.songName.text = item.mediaMetadata.title holder.songArtist.text = item.mediaMetadata.artist holder.indicator.text = - CalculationUtils.convertDurationToTimeStamp( - item.mediaMetadata.extras?.getLong("Duration")!! - ) + CalculationUtils.convertDurationToTimeStamp(item.mediaMetadata.durationMs!!) holder.songCover.load(item.mediaMetadata.artworkUri) { placeholderScaleToFit(R.drawable.ic_default_cover) crossfade(true) @@ -1529,7 +1526,7 @@ class FullBottomSheet if (!runnableRunning) return val position = CalculationUtils.convertDurationToTimeStamp(instance?.currentPosition ?: 0) - val duration = instance?.currentMediaItem?.mediaMetadata?.extras?.getLong("Duration") + val duration = instance?.currentMediaItem?.mediaMetadata?.durationMs if (duration != null && !isUserTracking) { bottomSheetFullSeekBar.max = duration.toInt() bottomSheetFullSeekBar.progress = instance?.currentPosition?.toInt() ?: 0 diff --git a/app/src/main/kotlin/org/akanework/gramophone/ui/fragments/DetailDialogFragment.kt b/app/src/main/kotlin/org/akanework/gramophone/ui/fragments/DetailDialogFragment.kt index 77ca60e26..1d6ae155b 100644 --- a/app/src/main/kotlin/org/akanework/gramophone/ui/fragments/DetailDialogFragment.kt +++ b/app/src/main/kotlin/org/akanework/gramophone/ui/fragments/DetailDialogFragment.kt @@ -67,7 +67,7 @@ class DetailDialogFragment : BaseFragment(false) { if (mediaMetadata.releaseYear != null || mediaMetadata.recordingYear != null) { yearTextView.text = (mediaMetadata.releaseYear ?: mediaMetadata.recordingYear)?.toString() } - durationTextView.text = convertDurationToTimeStamp(mediaMetadata.extras!!.getLong("Duration")) + durationTextView.text = convertDurationToTimeStamp(mediaMetadata.durationMs!!) mimeTypeTextView.text = mediaItem.localConfiguration?.mimeType ?: "(null)" pathTextView.text = mediaItem.getFile()?.path ?: mediaItem.requestMetadata.mediaUri?.toString() ?: "(null)"