8

Is there any way to use extras with the MediaMetadataCompat from the support library?

Using the MediaMetadata I can set extras, but with the compat one I cannot.

ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
gonzalomelov
  • 971
  • 1
  • 13
  • 30

2 Answers2

3

Hope it helps.

 import android.support.v4.media.session.MediaSessionCompat;

   private MediaSessionCompat mMediaSession;
    //init mediasesson
    mMediaSession = new MediaSessionCompat(getApplicationContext(), "AudioPlayer",new ComponentName(this, HeadsetNotificationBroadcast.class), null);
    //set the metadata
    mMediaSession.setMetadata(new MediaMetadataCompat.Builder()
                    .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, getSongDataHelper().getAlbumArt())
                    .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getSongDataHelper().getArtist())
                    .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getSongDataHelper().getAlbum())
                    .putString(MediaMetadataCompat.METADATA_KEY_TITLE, getSongDataHelper().getTitle())
                    .build());
Reyansh Mishra
  • 1,879
  • 1
  • 14
  • 26
2

I have copy-pasted our code. Please tell me if you can understand it.

private static MediaInfo toCastMediaMetadata(MediaMetadataCompat track, JSONObject customData) {
        MediaMetadata mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MUSIC_TRACK);
        mediaMetadata.putString(MediaMetadata.KEY_TITLE, track.getDescription().getTitle().toString());
        mediaMetadata.putString(MediaMetadata.KEY_SUBTITLE, track.getDescription().getSubtitle().toString());
        mediaMetadata.putString(MediaMetadata.KEY_ALBUM_ARTIST, track.getString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST));
        mediaMetadata.putString(MediaMetadata.KEY_ALBUM_TITLE, track.getString(MediaMetadataCompat.METADATA_KEY_ALBUM));
        WebImage image = new WebImage(new Uri.Builder().encodedPath(track.getString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI)).build());
        // First image is used by the receiver for showing the audio album art.
        mediaMetadata.addImage(image);
        // Second image is used by Cast Companion Library on the full screen activity that is shown
        // when the cast dialog is clicked.
        mediaMetadata.addImage(image);

        return new MediaInfo.Builder(
            track.getDescription().getExtras().getString(MutableMediaMetadataCompat.
                METADATA_KEY_TRACK_SOURCE)).setContentType(
            MIME_TYPE_AUDIO_MPEG).setStreamType(MediaInfo.STREAM_TYPE_BUFFERED).setMetadata(mediaMetadata).setCustomData(customData).build();
    }
gonzalomelov
  • 971
  • 1
  • 13
  • 30
  • Earlier I thought the same way but since tracks can be multiple and MediaMetadataCompat doesn't allow inserting array, so can't be used in all cases, also its seems to me MediaMetadataCompat lacks many important fields like mime type or may be I am missing something – ingsaurabh Jul 20 '17 at 06:42
  • 1
    Yeap. I don't fully remember but you're right, MediaMetadataCompat is missing several fields. We managed to make it work using a JsonObject serialized into one field that we wasn't used but I don't know if it works for you. Anyway, it's a workaround and not a standard solution. – gonzalomelov Jul 20 '17 at 15:57