Skip to content

Commit

Permalink
Simplify updating audio metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Jyotiraditya Panda <[email protected]>
  • Loading branch information
imjyotiraditya committed Nov 25, 2024
1 parent 433b107 commit 7135bff
Showing 1 changed file with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import androidx.preference.PreferenceManager
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.slider.Slider
import kotlin.io.path.Path
import kotlin.io.path.name
import org.akanework.gramophone.R
import org.akanework.gramophone.logic.getBooleanStrict
import org.akanework.gramophone.logic.playOrPause
Expand Down Expand Up @@ -166,28 +164,11 @@ class AudioPreviewActivity : AppCompatActivity() {

override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
if (mediaItem == null) return
audioTitle.text =
mediaItem.mediaMetadata.title
?: mediaItem.localConfiguration?.uri?.lastPathSegment?.let { Path(it) }?.name
artistTextView.text = mediaItem.mediaMetadata.artist
mediaItem.mediaMetadata.artworkData?.let {
val bitmap = BitmapFactory.decodeByteArray(it, 0, it.size)
albumArt.setImageBitmap(bitmap)
} ?: run {
albumArt.setImageResource(R.drawable.ic_default_cover)
}
updateMediaMetadata(mediaItem.mediaMetadata)
}

override fun onMediaMetadataChanged(mediaMetadata: MediaMetadata) {
audioTitle.text = mediaMetadata.title ?: player.currentMediaItem
?.localConfiguration?.uri?.lastPathSegment?.let { Path(it) }?.name
artistTextView.text = mediaMetadata.artist
mediaMetadata.artworkData?.let {
val bitmap = BitmapFactory.decodeByteArray(it, 0, it.size)
albumArt.setImageBitmap(bitmap)
} ?: run {
albumArt.setImageResource(R.drawable.ic_default_cover)
}
updateMediaMetadata(mediaMetadata)
}
})
playPauseButton.setOnClickListener {
Expand Down Expand Up @@ -289,4 +270,15 @@ class AudioPreviewActivity : AppCompatActivity() {
}
}
}

private fun updateMediaMetadata(mediaMetadata: MediaMetadata) {
audioTitle.text = mediaMetadata.title ?: getString(R.string.unknown_title)
artistTextView.text = mediaMetadata.artist ?: getString(R.string.unknown_artist)
mediaMetadata.artworkData?.let {
val bitmap = BitmapFactory.decodeByteArray(it, 0, it.size)
albumArt.setImageBitmap(bitmap)
} ?: run {
albumArt.setImageResource(R.drawable.ic_default_cover)
}
}
}

0 comments on commit 7135bff

Please sign in to comment.