Y
Y
Yar Rick2022-01-27 18:58:19
Android
Yar Rick, 2022-01-27 18:58:19

How to update metadata for playing media without interrupting playback?

I am working with androidx.media3 libraries and developing a service based on MediaSessionService. In my MediaSessionCallback callback, I'm trying to process a request to change the media rating:

override fun onSetRating(
    session: MediaSession,
    controller: ControllerInfo,
    rating: Rating
): ListenableFuture<SessionResult> {

    val item = session.player.currentMediaItem

    item?.let {
        val metadata  = it.mediaMetadata.buildUpon().setUserRating(rating).build()
        val mediaItem = it.buildUpon().setMediaMetadata(metadata).build()
        session.player.setMediaItem(mediaItem, false)
    }
    
    return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))
}

Everything works, but the sound breaks for a moment, which is not surprising, because I am essentially installing a new track in the player. The question is, how easy is it to update the current metadata in the player? Or a broader question - how should this feature work with a rating in MediaSession?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question