'Android : Update data on car audio

Sorry for my pore english.

I do a radio player. I can get curent name and title so every 15 seconds I read the new informations and display them on the main activity and in the notification. When I connect my phone to my car, the firdt artist is well displayed. When the second comes, name is correct on my phone but not updated on the car's screen. Eachtime the connection is stoped and restarted, name is correct but never updated when the next song comes.

My class implement AudioManager.

I use this code to update metadata :

mediaSession is a MediaSessionCompat

   mediaSession.setMetadata(new MediaMetadataCompat.Builder()
    .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, strArtist)
    .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, strAlbum)
    .putString(MediaMetadataCompat.METADATA_KEY_TITLE, strTitle)
    .build()); 

I think the update of mediaSession is not sent to the bluetooth device.

Is there a command to force the sync ?

Thank you for any help.

JC



Solution 1:[1]

You have to call MediaSession.setMetadata(MediaMetadataCompat metadata) every time track information changes. https://developer.android.com/guide/topics/media-apps/media-apps-overview#mediasession-and-mediacontroller To get the update for Metadata change you need to register for MediaController.Callback for calling MediaControllerCompat.registerCallback(ControllerCallback). So when you set new Metadata for MediaSession, this Metadata will be received in onMetadataChanged (MediaMetadata metadata) callback of MediaController.Callback (https://developer.android.com/reference/android/media/session/MediaController.Callback?hl=en#onMetadataChanged(android.media.MediaMetadata)) method.

Solution 2:[2]

You have to call MediaSession.setMetadata(MediaMetadataCompat metadata) every time track information changes. (call it in mediasession callbacks you onPlay/onPause/onStop from MediaSessionCompat.Callback) https://developer.android.com/guide/topics/media-apps/audio-app/mediasession-callbacks

Solution 3:[3]

I was facing this issue. And the problem was a bit. In My case, I was not setting the duration for the music. And always it was going to 0 only. After lots of research over google and stack overflow itself. I was not able to get the solution. But finally, I have found it and now it's working fine. If one of you also facing the same issue please go through with the below solution.

    private fun initMediaSessionMetadata(currentTrack: MusicModel?) {
    var duration =
        (currentTrack?.duration?.toLong() ?: mMediaPlayer?.duration?.toLong()) ?: 1000
    duration = if (duration <= 0) 1000 else duration
    val info = MediaMetadataCompat.Builder()
        .putString(MediaMetadata.METADATA_KEY_MEDIA_ID, currentTrack?.id.toString())
        .putString(MediaMetadata.METADATA_KEY_TITLE, currentTrack?.name)
        .putString(MediaMetadata.METADATA_KEY_ARTIST, currentTrack?.artists)
        .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, currentTrack?.thumbnail)
        .putLong(MediaMetadata.METADATA_KEY_DURATION, duration)
    val metadata = info.build()
    mediaSession?.setMetadata(metadata)
}

I hope you will able to crack it now. The following line is only the main problem for me
duration = if (duration <= 0) 1000 else duration
After this, My Code is working very well. If you have any query you can connect me via comment.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 A-run
Solution 2
Solution 3 Pushpendra