1--- 2title: Retrieving metadata 3--- 4 5## During playback ## 6 7The metadata of the media can be retrieved during playback in multiple ways. The 8most straightforward is to listen for the 9`Player.Listener#onMediaMetadataChanged` event; this will provide a 10[`MediaMetadata`][] object for use, which has fields such as `title` and 11`albumArtist`. Alternatively, calling `Player#getMediaMetadata` returns the same 12object. 13 14~~~ 15public void onMediaMetadataChanged(MediaMetadata mediaMetadata) { 16 if (mediaMetadata.title != null) { 17 handleTitle(mediaMetadata.title); 18 } 19} 20 21~~~ 22{: .language-java} 23 24If an application needs access to specific [`Metadata.Entry`][] objects, then it 25should add a `MetadataOutput` (for dynamic metadata delivered during 26playback) to the player. Alternatively, if there is a need to look at static 27metadata, this can be accessed through the `TrackSelections#getFormat`. Both of 28these options are used to populate the `Player#getMediaMetadata`. 29 30## Without playback ## 31 32If playback is not needed, it is more efficient to use the 33[`MetadataRetriever`][] to extract the metadata because it avoids having to 34create and prepare a player. 35 36~~~ 37ListenableFuture<TrackGroupArray> trackGroupsFuture = 38 MetadataRetriever.retrieveMetadata(context, mediaItem); 39Futures.addCallback( 40 trackGroupsFuture, 41 new FutureCallback<TrackGroupArray>() { 42 @Override 43 public void onSuccess(TrackGroupArray trackGroups) { 44 handleMetadata(trackGroups); 45 } 46 47 @Override 48 public void onFailure(Throwable t) { 49 handleFailure(t); 50 } 51 }, 52 executor); 53~~~ 54{: .language-java} 55 56## Motion photos ## 57 58It is also possible to extract the metadata of a motion photo, containing the 59image and video offset and length for example. The supported formats are: 60 61* JPEG motion photos recorded by Google Pixel and Samsung camera apps. This 62 format is playable by ExoPlayer and the associated metadata can therefore be 63 retrieved with a player or using the `MetadataRetriever`. 64* HEIC motion photos recorded by Google Pixel and Samsung camera apps. This 65 format is currently not playable by ExoPlayer and the associated metadata 66 should therefore be retrieved using the `MetadataRetriever`. 67 68For motion photos, the `TrackGroupArray` obtained with the `MetadataRetriever` 69contains a `TrackGroup` with a single `Format` enclosing a 70[`MotionPhotoMetadata`][] metadata entry. 71 72~~~ 73for (int i = 0; i < trackGroups.length; i++) { 74 TrackGroup trackGroup = trackGroups.get(i); 75 Metadata metadata = trackGroup.getFormat(0).metadata; 76 if (metadata != null && metadata.length() == 1) { 77 Metadata.Entry metadataEntry = metadata.get(0); 78 if (metadataEntry instanceof MotionPhotoMetadata) { 79 MotionPhotoMetadata motionPhotoMetadata = (MotionPhotoMetadata) metadataEntry; 80 handleMotionPhotoMetadata(motionPhotoMetadata); 81 } 82 } 83} 84~~~ 85{: .language-java} 86 87[`MediaMetadata`]: {{ site.exo_sdk }}/MediaMetadata.html 88[`Metadata.Entry`]: {{ site.exo_sdk }}/metadata/Metadata.Entry.html 89[`MetadataRetriever`]: {{ site.exo_sdk }}/MetadataRetriever.html 90[`MotionPhotoMetadata`]: {{ site.exo_sdk }}/metadata/mp4/MotionPhotoMetadata.html 91