Update androidx.media3 to 1.0.0-beta01

This commit is contained in:
vfsfitvnm 2022-06-17 13:46:06 +02:00
parent e0d0df804b
commit 2877009db4
2 changed files with 64 additions and 33 deletions

View file

@ -38,6 +38,7 @@ import androidx.media3.session.*
import androidx.media3.session.MediaNotification.ActionFactory import androidx.media3.session.MediaNotification.ActionFactory
import coil.Coil import coil.Coil
import coil.request.ImageRequest import coil.request.ImageRequest
import com.google.common.collect.ImmutableList
import com.google.common.util.concurrent.Futures import com.google.common.util.concurrent.Futures
import com.google.common.util.concurrent.ListenableFuture import com.google.common.util.concurrent.ListenableFuture
import it.vfsfitvnm.vimusic.Database import it.vfsfitvnm.vimusic.Database
@ -69,9 +70,7 @@ val CancelSleepTimerCommand = SessionCommand("CancelSleepTimerCommand", Bundle.E
@ExperimentalAnimationApi @ExperimentalAnimationApi
@ExperimentalFoundationApi @ExperimentalFoundationApi
class PlayerService : MediaSessionService(), MediaSession.MediaItemFiller, class PlayerService : MediaSessionService(), MediaSession.Callback, MediaNotification.Provider,
MediaNotification.Provider,
MediaSession.SessionCallback,
PlaybackStatsListener.Callback, Player.Listener { PlaybackStatsListener.Callback, Player.Listener {
companion object { companion object {
@ -119,10 +118,11 @@ class PlayerService : MediaSessionService(), MediaSession.MediaItemFiller,
.setAudioAttributes( .setAudioAttributes(
AudioAttributes.Builder() AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA) .setUsage(C.USAGE_MEDIA)
.setContentType(C.CONTENT_TYPE_MUSIC) .setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
.build(), .build(),
true true
) )
.setUsePlatformDiagnostics(false)
.build() .build()
player.repeatMode = preferences.repeatMode player.repeatMode = preferences.repeatMode
@ -132,8 +132,7 @@ class PlayerService : MediaSessionService(), MediaSession.MediaItemFiller,
mediaSession = MediaSession.Builder(this, player) mediaSession = MediaSession.Builder(this, player)
.withSessionActivity() .withSessionActivity()
.setSessionCallback(this) .setCallback(this)
.setMediaItemFiller(this)
.build() .build()
player.addListener(this) player.addListener(this)
@ -227,7 +226,10 @@ class PlayerService : MediaSessionService(), MediaSession.MediaItemFiller,
delay(delayMillis) delay(delayMillis)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
val notification = NotificationCompat.Builder(this@PlayerService, SleepTimerNotificationChannelId) val notification = NotificationCompat.Builder(
this@PlayerService,
SleepTimerNotificationChannelId
)
.setContentTitle("Sleep timer ended") .setContentTitle("Sleep timer ended")
.setPriority(NotificationCompat.PRIORITY_DEFAULT) .setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true) .setAutoCancel(true)
@ -244,7 +246,10 @@ class PlayerService : MediaSessionService(), MediaSession.MediaItemFiller,
} }
GetSleepTimerMillisLeftCommand -> { GetSleepTimerMillisLeftCommand -> {
return Futures.immediateFuture(sleepTimerRealtime?.let { return Futures.immediateFuture(sleepTimerRealtime?.let {
(SessionResult(SessionResult.RESULT_SUCCESS, bundleOf("millisLeft" to it - SystemClock.elapsedRealtime()))) (SessionResult(
SessionResult.RESULT_SUCCESS,
bundleOf("millisLeft" to it - SystemClock.elapsedRealtime())
))
} ?: SessionResult(SessionResult.RESULT_ERROR_INVALID_STATE)) } ?: SessionResult(SessionResult.RESULT_ERROR_INVALID_STATE))
} }
CancelSleepTimerCommand -> { CancelSleepTimerCommand -> {
@ -280,26 +285,32 @@ class PlayerService : MediaSessionService(), MediaSession.MediaItemFiller,
} }
} }
override fun fillInLocalConfiguration( override fun onAddMediaItems(
session: MediaSession, mediaSession: MediaSession,
controller: MediaSession.ControllerInfo, controller: MediaSession.ControllerInfo,
mediaItem: MediaItem mediaItems: MutableList<MediaItem>
): MediaItem { ): ListenableFuture<MutableList<MediaItem>> {
return mediaItem.buildUpon() return Futures.immediateFuture(
.setUri(mediaItem.mediaId) mediaItems.map { mediaItem ->
.setCustomCacheKey(mediaItem.mediaId) mediaItem.buildUpon()
.build() .setUri(mediaItem.mediaId)
.setCustomCacheKey(mediaItem.mediaId)
.build()
}.toMutableList()
)
} }
override fun createNotification( override fun createNotification(
mediaController: MediaController, session: MediaSession,
customLayout: ImmutableList<CommandButton>,
actionFactory: ActionFactory, actionFactory: ActionFactory,
onNotificationChangedCallback: MediaNotification.Provider.Callback onNotificationChangedCallback: MediaNotification.Provider.Callback
): MediaNotification { ): MediaNotification {
fun invalidate() { fun invalidate() {
onNotificationChangedCallback.onNotificationChanged( onNotificationChangedCallback.onNotificationChanged(
createNotification( createNotification(
mediaController, session,
customLayout,
actionFactory, actionFactory,
onNotificationChangedCallback onNotificationChangedCallback
) )
@ -309,10 +320,11 @@ class PlayerService : MediaSessionService(), MediaSession.MediaItemFiller,
fun NotificationCompat.Builder.addMediaAction( fun NotificationCompat.Builder.addMediaAction(
@DrawableRes resId: Int, @DrawableRes resId: Int,
@StringRes stringId: Int, @StringRes stringId: Int,
@Player.Command command: Long @Player.Command command: Int
): NotificationCompat.Builder { ): NotificationCompat.Builder {
return addAction( return addAction(
actionFactory.createMediaAction( actionFactory.createMediaAction(
mediaSession,
IconCompat.createWithResource(this@PlayerService, resId), IconCompat.createWithResource(this@PlayerService, resId),
getString(stringId), getString(stringId),
command command
@ -320,7 +332,7 @@ class PlayerService : MediaSessionService(), MediaSession.MediaItemFiller,
) )
} }
val mediaMetadata = mediaController.mediaMetadata val mediaMetadata = mediaSession.player.mediaMetadata
val builder = NotificationCompat.Builder(applicationContext, NotificationChannelId) val builder = NotificationCompat.Builder(applicationContext, NotificationChannelId)
.setContentTitle(mediaMetadata.title) .setContentTitle(mediaMetadata.title)
@ -331,8 +343,13 @@ class PlayerService : MediaSessionService(), MediaSession.MediaItemFiller,
.setShowWhen(false) .setShowWhen(false)
.setSmallIcon(R.drawable.app_icon) .setSmallIcon(R.drawable.app_icon)
.setOngoing(false) .setOngoing(false)
.setContentIntent(mediaController.sessionActivity) .setContentIntent(mediaSession.sessionActivity)
.setDeleteIntent(actionFactory.createMediaActionPendingIntent(ActionFactory.COMMAND_STOP)) .setDeleteIntent(
actionFactory.createMediaActionPendingIntent(
mediaSession,
Player.COMMAND_STOP.toLong()
)
)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setStyle( .setStyle(
androidx.media.app.NotificationCompat.MediaStyle() androidx.media.app.NotificationCompat.MediaStyle()
@ -342,25 +359,25 @@ class PlayerService : MediaSessionService(), MediaSession.MediaItemFiller,
.addMediaAction( .addMediaAction(
R.drawable.play_skip_back, R.drawable.play_skip_back,
R.string.media3_controls_seek_to_previous_description, R.string.media3_controls_seek_to_previous_description,
ActionFactory.COMMAND_SKIP_TO_PREVIOUS Player.COMMAND_SEEK_TO_PREVIOUS
).run { ).run {
if (mediaController.playbackState == Player.STATE_ENDED || !mediaController.playWhenReady) { if (mediaSession.player.playbackState == Player.STATE_ENDED || !mediaSession.player.playWhenReady) {
addMediaAction( addMediaAction(
R.drawable.play, R.drawable.play,
R.string.media3_controls_play_description, R.string.media3_controls_play_description,
ActionFactory.COMMAND_PLAY Player.COMMAND_PLAY_PAUSE
) )
} else { } else {
addMediaAction( addMediaAction(
R.drawable.pause, R.drawable.pause,
R.string.media3_controls_pause_description, R.string.media3_controls_pause_description,
ActionFactory.COMMAND_PAUSE Player.COMMAND_PLAY_PAUSE
) )
} }
}.addMediaAction( }.addMediaAction(
R.drawable.play_skip_forward, R.drawable.play_skip_forward,
R.string.media3_controls_seek_to_next_description, R.string.media3_controls_seek_to_next_description,
ActionFactory.COMMAND_SKIP_TO_NEXT Player.COMMAND_SEEK_TO_NEXT
) )
if (lastArtworkUri != mediaMetadata.artworkUri) { if (lastArtworkUri != mediaMetadata.artworkUri) {
@ -384,11 +401,13 @@ class PlayerService : MediaSessionService(), MediaSession.MediaItemFiller,
return MediaNotification(NotificationId, builder.build()) return MediaNotification(NotificationId, builder.build())
} }
override fun handleCustomAction( override fun handleCustomCommand(
mediaController: MediaController, session: MediaSession,
action: String, action: String,
extras: Bundle extras: Bundle
) = Unit ): Boolean {
return false
}
private fun createNotificationChannel() { private fun createNotificationChannel() {
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
@ -397,11 +416,23 @@ class PlayerService : MediaSessionService(), MediaSession.MediaItemFiller,
with(notificationManager) { with(notificationManager) {
if (getNotificationChannel(NotificationChannelId) == null) { if (getNotificationChannel(NotificationChannelId) == null) {
createNotificationChannel(NotificationChannel(NotificationChannelId, getString(R.string.default_notification_channel_name), NotificationManager.IMPORTANCE_LOW)) createNotificationChannel(
NotificationChannel(
NotificationChannelId,
getString(R.string.default_notification_channel_name),
NotificationManager.IMPORTANCE_LOW
)
)
} }
if (getNotificationChannel(SleepTimerNotificationChannelId) == null) { if (getNotificationChannel(SleepTimerNotificationChannelId) == null) {
createNotificationChannel(NotificationChannel(SleepTimerNotificationChannelId, "Sleep timer", NotificationManager.IMPORTANCE_DEFAULT)) createNotificationChannel(
NotificationChannel(
SleepTimerNotificationChannelId,
"Sleep timer",
NotificationManager.IMPORTANCE_DEFAULT
)
)
} }
} }
} }

View file

@ -35,7 +35,7 @@ dependencyResolutionManagement {
alias("room").to("androidx.room", "room-ktx").versionRef("room") alias("room").to("androidx.room", "room-ktx").versionRef("room")
alias("room-compiler").to("androidx.room", "room-compiler").versionRef("room") alias("room-compiler").to("androidx.room", "room-compiler").versionRef("room")
version("media3", "1.0.0-alpha03") version("media3", "1.0.0-beta01")
alias("media3-session").to("androidx.media3", "media3-session").versionRef("media3") alias("media3-session").to("androidx.media3", "media3-session").versionRef("media3")
alias("media3-exoplayer").to("androidx.media3", "media3-exoplayer").versionRef("media3") alias("media3-exoplayer").to("androidx.media3", "media3-exoplayer").versionRef("media3")