Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

MediaMetadata

class MediaMetadata : CustomVersionedParcelable
androidx.media2.common.MediaMetadata

Contains metadata about an item, such as the title, artist, etc. This is optional, but you'd better to provide this as much as possible when you're using media widget and/or session APIs.

The media widget components build its UI based on the metadata here. For an example, androidx.media2.widget.MediaControlView will show title from the metadata.

The androidx.media2.session.MediaLibraryService.MediaLibrarySession would require some metadata values when it provides MediaItems to androidx.media2.session.MediaBrowser.

Topics covered here:

  1. Media ID
  2. Browsable type
  3. Playable
  4. Duration
  5. User rating

Media ID

If set, the media ID must be the persistent key for the underlying media contents, so androidx.media2.session.MediaController and androidx.media2.session.MediaBrowser can store the information and reuse it later. Some APIs requires a media ID (e.g. androidx.media2.session.MediaController#setRating, so you'd better specify one.

Typical example of using media ID is the URI of the contents, but use it with the caution because the metadata is shared across the process in plain text.

The androidx.media2.session.MediaLibraryService.MediaLibrarySession would require it for the library root, so androidx.media2.session.MediaBrowser can call subsequent androidx.media2.session.MediaBrowser#getChildren with the ID.

Browsable type

Browsable defines whether the media item has children and type of children if any. With this, androidx.media2.session.MediaBrowser can know whether the subsequent androidx.media2.session.MediaBrowser#getChildren would successfully run.

The androidx.media2.session.MediaLibraryService.MediaLibrarySession would require the explicit browsable type for the media items returned by the androidx.media2.session.MediaLibraryService.MediaLibrarySession.MediaLibrarySessionCallback.

Playable type

Playable defines whether the media item can be played or not. It may be possible for a playlist to contain a media item which isn't playable in order to show a disabled media item.

The androidx.media2.session.MediaLibraryService.MediaLibrarySession would require the explicit playable value for the media items returned by the androidx.media2.session.MediaLibraryService.MediaLibrarySession.MediaLibrarySessionCallback.

  • Duration The duration is the length of the contents. The androidx.media2.session.MediaController can only get the duration through the metadata. This tells when would the playback ends, and also tells about the allowed range of androidx.media2.session.MediaController#seekTo(long).

    If it's not set by developer, androidx.media2.session.MediaSession would update the duration in the metadata with the SessionPlayer#getDuration().

  • User rating

    Prefer to have unrated user rating instead of null, so androidx.media2.session.MediaController can know the possible user rating type for calling androidx.media2.session.MediaController#setRating(String, Rating).

  • Summary

    Nested classes

    Use to build MediaMetadatax objects.

    Constants

    static Long

    The type of browsable that contains browsable items categorized by album.

    static Long

    The type of browsable that contains browsable items categorized by artist.

    static Long

    The type of browsable that contains browsable items categorized by genre.

    static Long

    The type of browsable that is unknown or contains media items of mixed types.

    static Long

    The type of browsable for non-browsable media item.

    static Long

    The type of browsable that contains browsable items categorized by playlist.

    static Long

    The type of browsable that only contains playable media items.

    static Long

    The type of browsable that contains browsable items categorized by year.

    static String

    The metadata key for a Long typed value to retrieve the information about whether the media is an advertisement.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the album title for the media.

    static String

    The metadata key for a Bitmap typed value to retrieve the information about the artwork for the album of the media's original source.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the artist for the album of the media's original source.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the Uri of the artwork for the album of the media's original source.

    static String

    The metadata key for a Bitmap typed value to retrieve the information about the artwork for the media.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the artist of the media.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about Uri of the artwork for the media.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the author of the media.

    static String

    The metadata key for a Long typed value to retrieve the information about the type of browsable.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the compilation status of the media.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the composer of the media.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the date the media was created or published.

    static String

    The metadata key for a Long typed value to retrieve the information about the disc number for the media's original source.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the description that is suitable for display to the user.

    static String

    The metadata key for a Bitmap typed value to retrieve the information about the icon or thumbnail that is suitable for display to the user.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the Uri of icon or thumbnail that is suitable for display to the user.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the subtitle that is suitable for display to the user.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the title that is suitable for display to the user.

    static String

    The metadata key for a Long typed value to retrieve the information about the download status of the media which will be used for later offline playback.

    static String

    The metadata key for a Long typed value to retrieve the information about the duration of the media in ms.

    static String

    A Bundle extra.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the genre of the media.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the media ID of the content.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the Uri of the content.

    static String

    The metadata key for a Long typed value to retrieve the information about the number of tracks in the media's original source.

    static String

    The metadata key for a Long typed value to retrieve the information about whether the media is playable.

    static String

    The metadata key for a Rating typed value to retrieve the information about the overall rating for the media.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the title of the media.

    static String

    The metadata key for a Long typed value to retrieve the information about the track number for the media.

    static String

    The metadata key for a Rating typed value to retrieve the information about the user's rating for the media.

    static String

    The metadata key for a CharSequence or String typed value to retrieve the information about the writer of the media.

    static String

    The metadata key for a Long typed value to retrieve the information about the year the media was created or published.

    static Long

    The status value to indicate the media item is downloaded for later offline playback.

    static Long

    The status value to indicate the media item is being downloaded.

    static Long

    The status value to indicate the media item is not downloaded.

    Public methods

    Boolean
    containsKey(@NonNull key: String)

    Returns true if the given key is contained in the metadata

    Bitmap?
    getBitmap(@NonNull key: String)

    Return a Bitmap for the given key or null if no bitmap exists for the given key.

    Bundle?

    Get the extra Bundle from the metadata object.

    Float
    getFloat(@NonNull key: String)

    Return the value associated with the given key, or 0.

    Long
    getLong(@NonNull key: String)

    Returns the value associated with the given key, or 0L if no long exists for the given key.

    String?

    Returns the media id, or null if the id doesn't exist.

    Rating?
    getRating(@NonNull key: String)

    Return a Rating for the given key or null if no rating exists for the given key.

    String?
    getString(@NonNull key: String)

    Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

    CharSequence?
    getText(@NonNull key: String)

    Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

    MutableSet<String!>

    Returns a Set containing the Strings used as keys in this metadata.

    Int

    Get the number of fields in this metadata.

    String

    Constants

    BROWSABLE_TYPE_ALBUMS

    static val BROWSABLE_TYPE_ALBUMS: Long

    The type of browsable that contains browsable items categorized by album.

    This value matches with the folder type 'Albums' as specified in the section 6.10.2.2 of the Bluetooth AVRCP 1.5.

    Value: 2

    BROWSABLE_TYPE_ARTISTS

    static val BROWSABLE_TYPE_ARTISTS: Long

    The type of browsable that contains browsable items categorized by artist.

    This value matches with the folder type 'Artists' as specified in the section 6.10.2.2 of the Bluetooth AVRCP 1.5.

    Value: 3

    BROWSABLE_TYPE_GENRES

    static val BROWSABLE_TYPE_GENRES: Long

    The type of browsable that contains browsable items categorized by genre.

    This value matches with the folder type 'Genres' as specified in the section 6.10.2.2 of the Bluetooth AVRCP 1.5.

    Value: 4

    BROWSABLE_TYPE_MIXED

    static val BROWSABLE_TYPE_MIXED: Long

    The type of browsable that is unknown or contains media items of mixed types.

    This value matches with the folder type 'Mixed' as specified in the section 6.10.2.2 of the Bluetooth AVRCP 1.5.

    Value: 0

    BROWSABLE_TYPE_NONE

    static val BROWSABLE_TYPE_NONE: Long

    The type of browsable for non-browsable media item.

    Value: -1

    BROWSABLE_TYPE_PLAYLISTS

    static val BROWSABLE_TYPE_PLAYLISTS: Long

    The type of browsable that contains browsable items categorized by playlist.

    This value matches with the folder type 'Playlists' as specified in the section 6.10.2.2 of the Bluetooth AVRCP 1.5.

    Value: 5

    BROWSABLE_TYPE_TITLES

    static val BROWSABLE_TYPE_TITLES: Long

    The type of browsable that only contains playable media items.

    This value matches with the folder type 'Titles' as specified in the section 6.10.2.2 of the Bluetooth AVRCP 1.5.

    Value: 1

    BROWSABLE_TYPE_YEARS

    static val BROWSABLE_TYPE_YEARS: Long

    The type of browsable that contains browsable items categorized by year.

    This value matches with the folder type 'Years' as specified in the section 6.10.2.2 of the Bluetooth AVRCP 1.5.

    Value: 6

    METADATA_KEY_ADVERTISEMENT

    static val METADATA_KEY_ADVERTISEMENT: String

    The metadata key for a Long typed value to retrieve the information about whether the media is an advertisement. A value of 0 indicates it is not an advertisement. A value of 1 or non-zero indicates it is an advertisement. If not specified, this value is set to 0 by default.

    Value: "androidx.media2.metadata.ADVERTISEMENT"

    METADATA_KEY_ALBUM

    static val METADATA_KEY_ALBUM: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the album title for the media.

    Value: android.media.MediaMetadata.METADATA_KEY_ALBUM

    METADATA_KEY_ALBUM_ART

    static val METADATA_KEY_ALBUM_ART: String

    The metadata key for a Bitmap typed value to retrieve the information about the artwork for the album of the media's original source. The artwork should be relatively small and may be scaled down if it is too large. For higher resolution artwork, METADATA_KEY_ALBUM_ART_URI should be used instead.

    Value: android.media.MediaMetadata.METADATA_KEY_ALBUM_ART

    METADATA_KEY_ALBUM_ARTIST

    static val METADATA_KEY_ALBUM_ARTIST: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the artist for the album of the media's original source.

    Value: android.media.MediaMetadata.METADATA_KEY_ALBUM_ARTIST

    METADATA_KEY_ALBUM_ART_URI

    static val METADATA_KEY_ALBUM_ART_URI: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the Uri of the artwork for the album of the media's original source.

    Value: android.media.MediaMetadata.METADATA_KEY_ALBUM_ART_URI

    METADATA_KEY_ART

    static val METADATA_KEY_ART: String

    The metadata key for a Bitmap typed value to retrieve the information about the artwork for the media. The artwork should be relatively small and may be scaled down if it is too large. For higher resolution artwork, METADATA_KEY_ART_URI should be used instead.

    Value: android.media.MediaMetadata.METADATA_KEY_ART

    METADATA_KEY_ARTIST

    static val METADATA_KEY_ARTIST: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the artist of the media.

    Value: android.media.MediaMetadata.METADATA_KEY_ARTIST

    METADATA_KEY_ART_URI

    static val METADATA_KEY_ART_URI: String

    The metadata key for a CharSequence or String typed value to retrieve the information about Uri of the artwork for the media.

    Value: android.media.MediaMetadata.METADATA_KEY_ART_URI

    METADATA_KEY_AUTHOR

    static val METADATA_KEY_AUTHOR: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the author of the media.

    Value: android.media.MediaMetadata.METADATA_KEY_AUTHOR

    METADATA_KEY_BROWSABLE

    static val METADATA_KEY_BROWSABLE: String

    The metadata key for a Long typed value to retrieve the information about the type of browsable. It should be one of the following:

    The values other than BROWSABLE_TYPE_NONE mean that the media item has children.[

    This matches with the bluetooth folder type of the media specified in the section 6.10.2.2 of the Bluetooth AVRCP 1.5.

    Value: "androidx.media2.metadata.BROWSABLE"

    See Also

    METADATA_KEY_COMPILATION

    static val METADATA_KEY_COMPILATION: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the compilation status of the media.

    Value: android.media.MediaMetadata.METADATA_KEY_COMPILATION

    METADATA_KEY_COMPOSER

    static val METADATA_KEY_COMPOSER: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the composer of the media.

    Value: android.media.MediaMetadata.METADATA_KEY_COMPOSER

    METADATA_KEY_DATE

    static val METADATA_KEY_DATE: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the date the media was created or published. The format is unspecified but RFC 3339 is recommended.

    Value: android.media.MediaMetadata.METADATA_KEY_DATE

    METADATA_KEY_DISC_NUMBER

    static val METADATA_KEY_DISC_NUMBER: String

    The metadata key for a Long typed value to retrieve the information about the disc number for the media's original source.

    Value: android.media.MediaMetadata.METADATA_KEY_DISC_NUMBER

    METADATA_KEY_DISPLAY_DESCRIPTION

    static val METADATA_KEY_DISPLAY_DESCRIPTION: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the description that is suitable for display to the user. When displaying more information for media described by this metadata, this should be preferred to other fields if present.

    Value: android.media.MediaMetadata.METADATA_KEY_DISPLAY_DESCRIPTION

    METADATA_KEY_DISPLAY_ICON

    static val METADATA_KEY_DISPLAY_ICON: String

    The metadata key for a Bitmap typed value to retrieve the information about the icon or thumbnail that is suitable for display to the user. When displaying an icon for media described by this metadata, this should be preferred to other fields if present.

    The icon should be relatively small and may be scaled down if it is too large. For higher resolution artwork, METADATA_KEY_DISPLAY_ICON_URI should be used instead.

    Value: android.media.MediaMetadata.METADATA_KEY_DISPLAY_ICON

    METADATA_KEY_DISPLAY_ICON_URI

    static val METADATA_KEY_DISPLAY_ICON_URI: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the Uri of icon or thumbnail that is suitable for display to the user. When displaying more information for media described by this metadata, the display description should be preferred to other fields when present.

    Value: android.media.MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI

    METADATA_KEY_DISPLAY_SUBTITLE

    static val METADATA_KEY_DISPLAY_SUBTITLE: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the subtitle that is suitable for display to the user. When displaying a second line for media described by this metadata, this should be preferred to other fields if present.

    Value: android.media.MediaMetadata.METADATA_KEY_DISPLAY_SUBTITLE

    METADATA_KEY_DISPLAY_TITLE

    static val METADATA_KEY_DISPLAY_TITLE: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the title that is suitable for display to the user. It will generally be the same as METADATA_KEY_TITLE but may differ for some formats. When displaying media described by this metadata, this should be preferred if present.

    Value: android.media.MediaMetadata.METADATA_KEY_DISPLAY_TITLE

    METADATA_KEY_DOWNLOAD_STATUS

    static val METADATA_KEY_DOWNLOAD_STATUS: String

    The metadata key for a Long typed value to retrieve the information about the download status of the media which will be used for later offline playback. It should be one of the following:

    Value: "androidx.media2.metadata.DOWNLOAD_STATUS"

    METADATA_KEY_DURATION

    static val METADATA_KEY_DURATION: String

    The metadata key for a Long typed value to retrieve the information about the duration of the media in ms. A negative duration indicates that the duration is unknown (or infinite).

    Value: android.media.MediaMetadata.METADATA_KEY_DURATION

    METADATA_KEY_EXTRAS

    static val METADATA_KEY_EXTRAS: String

    A Bundle extra.

    Value: "androidx.media2.metadata.EXTRAS"

    METADATA_KEY_GENRE

    static val METADATA_KEY_GENRE: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the genre of the media.

    Value: android.media.MediaMetadata.METADATA_KEY_GENRE

    METADATA_KEY_MEDIA_ID

    static val METADATA_KEY_MEDIA_ID: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the media ID of the content. This value is specific to the service providing the content. If used, this should be a persistent key for the underlying content. This ID is used by androidx.media2.session.MediaController and androidx.media2.session.MediaBrowser.

    Value: android.media.MediaMetadata.METADATA_KEY_MEDIA_ID

    METADATA_KEY_MEDIA_URI

    static val METADATA_KEY_MEDIA_URI: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the Uri of the content. This value is specific to the service providing the content.

    Value: android.media.MediaMetadata.METADATA_KEY_MEDIA_URI

    METADATA_KEY_NUM_TRACKS

    static val METADATA_KEY_NUM_TRACKS: String

    The metadata key for a Long typed value to retrieve the information about the number of tracks in the media's original source.

    Value: android.media.MediaMetadata.METADATA_KEY_NUM_TRACKS

    METADATA_KEY_PLAYABLE

    static val METADATA_KEY_PLAYABLE: String

    The metadata key for a Long typed value to retrieve the information about whether the media is playable. A value of 0 indicates it is not a playable item. A value of 1 or non-zero indicates it is playable.

    Value: "androidx.media2.metadata.PLAYABLE"

    See Also

    METADATA_KEY_RATING

    static val METADATA_KEY_RATING: String

    The metadata key for a Rating typed value to retrieve the information about the overall rating for the media.

    Value: android.media.MediaMetadata.METADATA_KEY_RATING

    METADATA_KEY_TITLE

    static val METADATA_KEY_TITLE: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the title of the media.

    Value: android.media.MediaMetadata.METADATA_KEY_TITLE

    METADATA_KEY_TRACK_NUMBER

    static val METADATA_KEY_TRACK_NUMBER: String

    The metadata key for a Long typed value to retrieve the information about the track number for the media.

    Value: android.media.MediaMetadata.METADATA_KEY_TRACK_NUMBER

    METADATA_KEY_USER_RATING

    static val METADATA_KEY_USER_RATING: String

    The metadata key for a Rating typed value to retrieve the information about the user's rating for the media. Prefer to have unrated user rating instead of null, so androidx.media2.session.MediaController can know the possible user rating type.

    Value: android.media.MediaMetadata.METADATA_KEY_USER_RATING

    See Also

    METADATA_KEY_WRITER

    static val METADATA_KEY_WRITER: String

    The metadata key for a CharSequence or String typed value to retrieve the information about the writer of the media.

    Value: android.media.MediaMetadata.METADATA_KEY_WRITER

    METADATA_KEY_YEAR

    static val METADATA_KEY_YEAR: String

    The metadata key for a Long typed value to retrieve the information about the year the media was created or published.

    Value: android.media.MediaMetadata.METADATA_KEY_YEAR

    STATUS_DOWNLOADED

    static val STATUS_DOWNLOADED: Long

    The status value to indicate the media item is downloaded for later offline playback.

    Value: 2

    STATUS_DOWNLOADING

    static val STATUS_DOWNLOADING: Long

    The status value to indicate the media item is being downloaded.

    Value: 1

    STATUS_NOT_DOWNLOADED

    static val STATUS_NOT_DOWNLOADED: Long

    The status value to indicate the media item is not downloaded.

    Value: 0

    Public methods

    containsKey

    fun containsKey(@NonNull key: String): Boolean

    Returns true if the given key is contained in the metadata

    Parameters
    key String: a String key
    Return
    Boolean: true if the key exists in this metadata, false otherwise

    getBitmap

    @Nullable fun getBitmap(@NonNull key: String): Bitmap?

    Return a Bitmap for the given key or null if no bitmap exists for the given key.

    Parameters
    key String: The key the value is stored under
    Return
    Bitmap?: A Bitmap or null

    getExtras

    @Nullable fun getExtras(): Bundle?

    Get the extra Bundle from the metadata object.

    Return
    Bundle?: A Bundle or null

    getFloat

    fun getFloat(@NonNull key: String): Float

    Return the value associated with the given key, or 0.0f if no long exists for the given key.

    Parameters
    key String: The key the value is stored under
    Return
    Float: a float value

    getLong

    fun getLong(@NonNull key: String): Long

    Returns the value associated with the given key, or 0L if no long exists for the given key.

    Parameters
    key String: The key the value is stored under
    Return
    Long: a long value

    getMediaId

    @Nullable fun getMediaId(): String?

    Returns the media id, or null if the id doesn't exist.

    This is equivalent to the getString(String) with the METADATA_KEY_MEDIA_ID.

    Return
    String?: media id. Can be null

    getRating

    @Nullable fun getRating(@NonNull key: String): Rating?

    Return a Rating for the given key or null if no rating exists for the given key.

    For the METADATA_KEY_USER_RATING, A null return value means that user rating cannot be set by androidx.media2.session.MediaController.

    Parameters
    key String: The key the value is stored under
    Return
    Rating?: A Rating or null

    getString

    @Nullable fun getString(@NonNull key: String): String?

    Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

    Parameters
    key String: The key the value is stored under
    Return
    String?: a String value, or null

    getText

    @Nullable fun getText(@NonNull key: String): CharSequence?

    Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

    Parameters
    key String: The key the value is stored under
    Return
    CharSequence?: a CharSequence value, or null

    keySet

    @NonNull fun keySet(): MutableSet<String!>

    Returns a Set containing the Strings used as keys in this metadata.

    Return
    MutableSet<String!>: a Set of String keys

    size

    fun size(): Int

    Get the number of fields in this metadata.

    Return
    Int: The number of fields in the metadata.

    toString

    fun toString(): String