chore: simplify and fix some mediasessioncompat regressions

This commit is contained in:
Bnyro 2023-08-10 13:33:52 +02:00
parent 698f783df3
commit 6a341f784b
2 changed files with 88 additions and 61 deletions

View File

@ -0,0 +1,58 @@
package com.github.libretube.extensions
import android.graphics.Bitmap
import android.support.v4.media.MediaMetadataCompat
import androidx.media3.common.C
import androidx.media3.common.MediaMetadata
import androidx.media3.session.MediaConstants
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
fun MediaMetadata.toMediaMetadataCompat(duration: Long, thumbnail: Bitmap?): MediaMetadataCompat {
val builder = MediaMetadataCompat.Builder()
title?.let {
builder.putText(MediaMetadataCompat.METADATA_KEY_TITLE, it)
builder.putText(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, it)
}
subtitle?.let {
builder.putText(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, it)
}
description?.let {
builder.putText(MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION, it)
}
artist?.let {
builder.putText(MediaMetadataCompat.METADATA_KEY_ARTIST, it)
}
albumTitle?.let {
builder.putText(MediaMetadataCompat.METADATA_KEY_ALBUM, it)
}
albumArtist?.let {
builder.putText(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, it)
}
recordingYear?.toLong()?.let {
builder.putLong(MediaMetadataCompat.METADATA_KEY_YEAR, it)
}
artworkUri?.toString()?.let {
builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI, it)
builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, it)
}
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, thumbnail)
if (duration != C.TIME_UNSET) {
builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration)
}
mediaType?.toLong()?.let {
builder.putLong(MediaConstants.EXTRAS_KEY_MEDIA_TYPE_COMPAT, it)
}
return builder.build()
}

View File

@ -10,7 +10,6 @@ import android.content.IntentFilter
import android.graphics.Bitmap import android.graphics.Bitmap
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.support.v4.media.MediaMetadataCompat
import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat import android.support.v4.media.session.PlaybackStateCompat
import android.util.Log import android.util.Log
@ -20,11 +19,9 @@ import androidx.core.app.PendingIntentCompat
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.toBitmap import androidx.core.graphics.drawable.toBitmap
import androidx.media.app.NotificationCompat.MediaStyle import androidx.media.app.NotificationCompat.MediaStyle
import androidx.media3.common.C
import androidx.media3.common.MediaMetadata import androidx.media3.common.MediaMetadata
import androidx.media3.common.Player import androidx.media3.common.Player
import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.session.MediaConstants
import coil.request.ImageRequest import coil.request.ImageRequest
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
@ -32,6 +29,7 @@ import com.github.libretube.constants.PLAYER_CHANNEL_ID
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.seekBy import com.github.libretube.extensions.seekBy
import com.github.libretube.extensions.toMediaMetadataCompat
import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.ImageHelper import com.github.libretube.helpers.ImageHelper
import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper
@ -218,89 +216,60 @@ class NowPlayingNotification(
mediaSession = MediaSessionCompat(context, TAG()) mediaSession = MediaSessionCompat(context, TAG())
mediaSession.setCallback(sessionCallback) mediaSession.setCallback(sessionCallback)
mediaSession.setPlaybackState(playbackState) mediaSession.setPlaybackState(playbackState)
mediaSession.setMetadata(getMetadataFromPlayer(player.mediaMetadata))
updateSessionMetadata()
val playerStateListener = object : Player.Listener { val playerStateListener = object : Player.Listener {
override fun onIsPlayingChanged(isPlaying: Boolean) { override fun onIsPlayingChanged(isPlaying: Boolean) {
super.onIsPlayingChanged(isPlaying) super.onIsPlayingChanged(isPlaying)
val newPlaybackState = if (isPlaying) { updateIsPlaying(isPlaying)
createPlaybackState(PlaybackStateCompat.STATE_PLAYING)
} else {
createPlaybackState(PlaybackStateCompat.STATE_PAUSED)
} }
override fun onPlaybackStateChanged(playbackState: Int) {
super.onPlaybackStateChanged(playbackState)
if (playbackState == Player.STATE_BUFFERING) {
val newPlaybackState = createPlaybackState(PlaybackStateCompat.STATE_BUFFERING)
mediaSession.setPlaybackState(newPlaybackState) mediaSession.setPlaybackState(newPlaybackState)
mediaSession.setMetadata(getMetadataFromPlayer(player.mediaMetadata)) }
if (playbackState == Player.STATE_READY) {
updateSessionMetadata()
updateIsPlaying(player.isPlaying)
}
} }
override fun onIsLoadingChanged(isLoading: Boolean) { override fun onIsLoadingChanged(isLoading: Boolean) {
super.onIsLoadingChanged(isLoading) super.onIsLoadingChanged(isLoading)
if (!isLoading) { if (!isLoading) {
mediaSession.setMetadata(getMetadataFromPlayer(player.mediaMetadata)) updateSessionMetadata()
} }
} }
override fun onMediaMetadataChanged(mediaMetadata: MediaMetadata) { override fun onMediaMetadataChanged(mediaMetadata: MediaMetadata) {
super.onMediaMetadataChanged(mediaMetadata) super.onMediaMetadataChanged(mediaMetadata)
mediaSession.setMetadata(getMetadataFromPlayer(mediaMetadata)) updateSessionMetadata(mediaMetadata)
} }
} }
player.addListener(playerStateListener) player.addListener(playerStateListener)
} }
private fun getMetadataFromPlayer(metadata: MediaMetadata): MediaMetadataCompat { private fun updateSessionMetadata(metadata: MediaMetadata? = null) {
val builder = MediaMetadataCompat.Builder() val data = metadata ?: player.mediaMetadata
val newMetadata = data.toMediaMetadataCompat(player.duration, notificationBitmap)
metadata.title?.let { mediaSession.setMetadata(newMetadata)
builder.putText(MediaMetadataCompat.METADATA_KEY_TITLE, it)
builder.putText(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, it)
} }
metadata.subtitle?.let { private fun updateIsPlaying(isPlaying: Boolean) {
builder.putText(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, it) val newPlaybackState = if (isPlaying) {
createPlaybackState(PlaybackStateCompat.STATE_PLAYING)
} else {
createPlaybackState(PlaybackStateCompat.STATE_PAUSED)
} }
mediaSession.setPlaybackState(newPlaybackState)
metadata.description?.let {
builder.putText(MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION, it)
}
metadata.artist?.let {
builder.putText(MediaMetadataCompat.METADATA_KEY_ARTIST, it)
}
metadata.albumTitle?.let {
builder.putText(MediaMetadataCompat.METADATA_KEY_ALBUM, it)
}
metadata.albumArtist?.let {
builder.putText(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, it)
}
metadata.recordingYear?.toLong()?.let {
builder.putLong(MediaMetadataCompat.METADATA_KEY_YEAR, it)
}
metadata.artworkUri?.toString()?.let {
builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI, it)
builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, it)
}
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, notificationBitmap)
val playerDuration = player.duration
if (playerDuration != C.TIME_UNSET) {
builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, playerDuration)
}
metadata.mediaType?.toLong()?.let {
builder.putLong(MediaConstants.EXTRAS_KEY_MEDIA_TYPE_COMPAT, it)
}
return builder.build()
} }
private fun createPlaybackState(@PlaybackStateCompat.State state: Int): PlaybackStateCompat { private fun createPlaybackState(@PlaybackStateCompat.State state: Int): PlaybackStateCompat {
@ -417,7 +386,7 @@ class NowPlayingNotification(
} }
} }
.build() .build()
mediaSession.setMetadata(getMetadataFromPlayer(player.mediaMetadata)) updateSessionMetadata()
nManager.notify(PLAYER_NOTIFICATION_ID, notification) nManager.notify(PLAYER_NOTIFICATION_ID, notification)
} }