androidx / media

Jetpack Media3 support libraries for media use cases, including ExoPlayer, an extensible media player for Android
https://developer.android.com/media/media3
Apache License 2.0
1.62k stars 384 forks source link

Lock screen control not showing song details as shown in notification #199

Open pawaom opened 1 year ago

pawaom commented 1 year ago

Lock screen control not showing song details as shown in notification

this is the notification screen

Screenshot_20221029-095614_MixThreeMusicPlayer_resized

while this is the lockscreen

Screenshot_20221029-095801_One UI Home (1)

for the same song

This is the code I am trying , its really a basic implementation of the media3 mediasession

class PlaybackService : MediaLibraryService() {
    private lateinit var mediaSession: MediaLibrarySession
    lateinit var exoPlayer: ExoPlayer
    //MediaSession
    private val librarySessionCallback = CustomMediaLibrarySessionCallback()
    override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaLibrarySession? {
        return  mediaSession
    }

    override fun onCreate() {
        super.onCreate()
        exoPlayer = ExoPlayer.Builder(this).build()
        mediaSession =
            MediaLibrarySession.Builder(this, exoPlayer, librarySessionCallback).build()
    }
    private inner class CustomMediaLibrarySessionCallback : MediaLibrarySession.Callback {
        override fun onAddMediaItems(
            mediaSession: MediaSession,
            controller: MediaSession.ControllerInfo,
            mediaItems: MutableList<MediaItem>
        ): ListenableFuture<MutableList<MediaItem>> {
            val updatedMediaItems = mediaItems.map { it.buildUpon().setUri(it.mediaId).build() }.toMutableList()
            return Futures.immediateFuture(updatedMediaItems)
        }
    }

    override fun onDestroy() {
        exoPlayer.release()
        mediaSession.release()
        super.onDestroy()
    }
}

and

class MainActivity : ComponentActivity() {
    private lateinit var controllerFuture: ListenableFuture<MediaController>
    private lateinit var controller: MediaController
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            SimpleOwnGithubMedia3PlayerTheme {
                Box(contentAlignment = Alignment.Center, modifier = Modifier.fillMaxSize()) {
                    Button(onClick = {
                        //val url = "android.resource://$packageName/${R.raw.test}"
                        val url = "https://download.samplelib.com/mp3/sample-15s.mp3"
**//we changed the song here, but the result is same for any song** 

                        play(url)

                    }) {
                        Text(text = "Play")
                    }

                }

            }
        }
    }

    override fun onStart() {
        super.onStart()
        val sessionToken = SessionToken(this, ComponentName(this, PlaybackService::class.java))
        controllerFuture = MediaController.Builder(this, sessionToken).buildAsync()
        controllerFuture.addListener(
            {
                controller = controllerFuture.get()
                initController()
            },
            MoreExecutors.directExecutor()
        )
    }

    override fun onStop() {
        MediaController.releaseFuture(controllerFuture)
        super.onStop()
    }

    private fun initController() {
        //controller.playWhenReady = true
        controller.addListener(object : Player.Listener {

            override fun onMediaMetadataChanged(mediaMetadata: MediaMetadata) {
                super.onMediaMetadataChanged(mediaMetadata)
                log("onMediaMetadataChanged=$mediaMetadata")
            }

            override fun onIsPlayingChanged(isPlaying: Boolean) {
                super.onIsPlayingChanged(isPlaying)
                log("onIsPlayingChanged=$isPlaying")
            }

            override fun onPlaybackStateChanged(playbackState: Int) {
                super.onPlaybackStateChanged(playbackState)
                log("onPlaybackStateChanged=${getStateName(playbackState)}")
            }

            override fun onPlayerError(error: PlaybackException) {
                super.onPlayerError(error)
                log("onPlayerError=${error.stackTraceToString()}")
            }

            override fun onPlayerErrorChanged(error: PlaybackException?) {
                super.onPlayerErrorChanged(error)
                log("onPlayerErrorChanged=${error?.stackTraceToString()}")
            }
        })
        log("start=${getStateName(controller.playbackState)}")
        log("COMMAND_PREPARE=${controller.isCommandAvailable(COMMAND_PREPARE)}")
        log("COMMAND_SET_MEDIA_ITEM=${controller.isCommandAvailable(COMMAND_SET_MEDIA_ITEM)}")
        log("COMMAND_PLAY_PAUSE=${controller.isCommandAvailable(COMMAND_PLAY_PAUSE)}")
    }

    private fun play(url: String) {
        log("play($url)")
        log("before=${getStateName(controller.playbackState)}")
       // controller.setMediaItem(MediaItem.fromUri(url))
        //we changed here
        val media = MediaItem.Builder().setMediaId(url).build()
        controller.setMediaItem(media)
        controller.prepare()
        controller.play()
        log("after=${getStateName(controller.playbackState)}")
    }

    private fun getStateName(i: Int): String? {
        return when (i) {
            1 -> "STATE_IDLE"
            2 -> "STATE_BUFFERING"
            3 -> "STATE_READY"
            4 -> "STATE_ENDED"
            else -> null
        }
    }

    private fun log(message: String) {
        Log.e("=====[TestMedia]=====", message)
    }

}

What should we do to show the song details on the lock screen also

Detials

Android studio dolphin

Tablet: Samsung Galaxy tab A7 lite Android version 11

tianyif commented 1 year ago

Hi @pawaom,

Thanks for reporting! However, the described issue was not reproducible on our demo-session app. The details of the reproduction:

So this looks like an app specific issue. Unfortunately we can't give 1:1 support for solving app specific issues. We'll leave this issue open for ~2 weeks in case anyone wishes to answer it here. If you think this is a problem with Media library, please narrow down your issue to describe the Media library behaviour that doesn't match your expectations.

pawaom commented 1 year ago

thanks for the reply i will check if this is a samsung specific issue, and get back to you, just to clarify did you try the above code I have shared or the mediasession demo code, that code works fine , however the above code which I had found on Stackoverflow which at best can be described as a basic implementation of mediasession causes the issue, I want to know what should we do to show the notification properly on lock screen also, I dont want a app specific help , but the general code that we need to implement the notification properly using media3 do we need to override any specific method

We have referred to this video

https://youtu.be/sTIBDcyCmCg?t=449

and this

https://developer.android.com/guide/topics/media/media3/getting-started/playing-in-background#notification

Which mentions

For example, a music streaming app using a MediaSessionService would create a MediaNotification that displays the title, artist, and album art for the current song playing alongside playback controls based on your MediaSession configuration.

But the lockscreen notification does not work as per expected behavior ,

Do we need to make some changes , if so , there is no documentation about that.