Example usage for android.support.v4.media MediaMetadataCompat getDescription

List of usage examples for android.support.v4.media MediaMetadataCompat getDescription

Introduction

In this page you can find the example usage for android.support.v4.media MediaMetadataCompat getDescription.

Prototype

public MediaDescriptionCompat getDescription() 

Source Link

Document

Returns a simple description of this metadata for display purposes.

Usage

From source file:com.bayapps.android.robophish.model.MusicProvider.java

private MediaBrowserCompat.MediaItem createMediaItem(MediaMetadataCompat metadata) {
    // Since mediaMetadata fields are immutable, we need to create a copy, so we
    // can set a hierarchy-aware mediaID. We will need to know the media hierarchy
    // when we get a onPlayFromMusicID call, so we can create the proper queue based
    // on where the music was selected from (by artist, by genre, random, etc)
    String title = metadata.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID);

    String venue = metadata.getString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE);

    String showId = metadata.getString(MediaMetadataCompat.METADATA_KEY_COMPILATION);
    String hierarchyAwareMediaID = MediaIDHelper.createMediaID(metadata.getDescription().getMediaId(),
            MEDIA_ID_TRACKS_BY_SHOW, showId);
    MediaMetadataCompat copy = new MediaMetadataCompat.Builder(metadata)
            .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, hierarchyAwareMediaID)
            .putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, venue).build();

    return new MediaBrowserCompat.MediaItem(copy.getDescription(), MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);

}

From source file:com.murati.oszk.audiobook.model.MusicProvider.java

private MediaBrowserCompat.MediaItem createTrackItem(MediaMetadataCompat metadata) {
    // Since mediaMetadata fields are immutable, we need to create a copy, so we
    // can set a hierarchy-aware mediaID. We will need to know the media hierarchy
    // when we get a onPlayFromMusicID call, so we can create the proper queue based
    // on where the music was selected from (by artist, by genre, random, etc)
    String ebook = metadata.getString(MediaMetadataCompat.METADATA_KEY_ALBUM);
    String hierarchyAwareMediaID = MediaIDHelper.createMediaID(metadata.getDescription().getMediaId(),
            MEDIA_ID_BY_EBOOK, ebook);//  w w  w. ja v a2s .c o  m
    MediaMetadataCompat copy = new MediaMetadataCompat.Builder(metadata)
            .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, hierarchyAwareMediaID).build();
    return new MediaBrowserCompat.MediaItem(copy.getDescription(), MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);

}

From source file:com.example.android.mediabrowserservice.MusicService.java

/**
 * Actual implementation of onLoadChildren that assumes that MusicProvider is already
 * initialized.//ww  w .j ava  2s .  co  m
 */
private void loadChildrenImpl(final String parentMediaId,
        final Result<List<MediaBrowserCompat.MediaItem>> result) {
    LogHelper.d(TAG, "OnLoadChildren: parentMediaId=", parentMediaId);

    List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

    if (MEDIA_ID_ROOT.equals(parentMediaId)) {
        LogHelper.d(TAG, "OnLoadChildren.ROOT");
        mediaItems.add(new MediaBrowserCompat.MediaItem(
                new MediaDescriptionCompat.Builder().setMediaId(MEDIA_ID_MUSICS_BY_GENRE)
                        .setTitle(getString(R.string.browse_genres))
                        .setIconUri(Uri.parse("android.resource://"
                                + "com.example.android.mediabrowserservice/drawable/ic_by_genre"))
                        .setSubtitle(getString(R.string.browse_genre_subtitle)).build(),
                MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));

    } else if (MEDIA_ID_MUSICS_BY_GENRE.equals(parentMediaId)) {
        LogHelper.d(TAG, "OnLoadChildren.GENRES");
        for (String genre : mMusicProvider.getGenres()) {
            MediaBrowserCompat.MediaItem item = new MediaBrowserCompat.MediaItem(
                    new MediaDescriptionCompat.Builder()
                            .setMediaId(createBrowseCategoryMediaID(MEDIA_ID_MUSICS_BY_GENRE, genre))
                            .setTitle(genre)
                            .setSubtitle(getString(R.string.browse_musics_by_genre_subtitle, genre)).build(),
                    MediaBrowserCompat.MediaItem.FLAG_BROWSABLE);
            mediaItems.add(item);
        }

    } else if (parentMediaId.startsWith(MEDIA_ID_MUSICS_BY_GENRE)) {
        String genre = MediaIDHelper.getHierarchy(parentMediaId)[1];
        LogHelper.d(TAG, "OnLoadChildren.SONGS_BY_GENRE  genre=", genre);
        for (MediaMetadataCompat track : mMusicProvider.getMusicsByGenre(genre)) {
            // Since mediaMetadata fields are immutable, we need to create a copy, so we
            // can set a hierarchy-aware mediaID. We will need to know the media hierarchy
            // when we get a onPlayFromMusicID call, so we can create the proper queue based
            // on where the music was selected from (by artist, by genre, random, etc)
            String hierarchyAwareMediaID = MediaIDHelper.createMediaID(track.getDescription().getMediaId(),
                    MEDIA_ID_MUSICS_BY_GENRE, genre);
            MediaMetadataCompat trackCopy = new MediaMetadataCompat.Builder(track)
                    .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, hierarchyAwareMediaID).build();
            MediaBrowserCompat.MediaItem bItem = new MediaBrowserCompat.MediaItem(trackCopy.getDescription(),
                    MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);
            mediaItems.add(bItem);
        }
    } else {
        LogHelper.w(TAG, "Skipping unmatched parentMediaId: ", parentMediaId);
    }
    LogHelper.d(TAG, "OnLoadChildren sending ", mediaItems.size(), " results for ", parentMediaId);
    result.sendResult(mediaItems);
}

From source file:com.example.android.supportv4.media.MediaBrowserServiceSupport.java

/**
 * Actual implementation of onLoadChildren that assumes that MusicProvider is already
 * initialized./*from   w  ww  .j a  v  a  2 s .c o  m*/
 */
private void loadChildrenImpl(final String parentMediaId, final Result<List<MediaItem>> result,
        final Bundle options) {
    Log.d(TAG, "OnLoadChildren: parentMediaId=" + parentMediaId + ", options=" + options);

    int page = -1;
    int pageSize = -1;

    if (options != null && (options.containsKey(MediaBrowserCompat.EXTRA_PAGE)
            || options.containsKey(MediaBrowserCompat.EXTRA_PAGE_SIZE))) {
        page = options.getInt(MediaBrowserCompat.EXTRA_PAGE, -1);
        pageSize = options.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1);

        if (page < 0 || pageSize < 1) {
            result.sendResult(new ArrayList<MediaItem>());
            return;
        }
    }

    int fromIndex = page == -1 ? 0 : page * pageSize;
    int toIndex = 0;

    List<MediaItem> mediaItems = new ArrayList<>();

    if (MEDIA_ID_ROOT.equals(parentMediaId)) {
        Log.d(TAG, "OnLoadChildren.ROOT");
        if (page <= 0) {
            mediaItems.add(new MediaItem(
                    new MediaDescriptionCompat.Builder().setMediaId(MEDIA_ID_MUSICS_BY_GENRE)
                            .setTitle(getString(R.string.browse_genres))
                            .setIconUri(Uri.parse("android.resource://"
                                    + "com.example.android.supportv4.media/drawable/ic_by_genre"))
                            .setSubtitle(getString(R.string.browse_genre_subtitle)).build(),
                    MediaItem.FLAG_BROWSABLE));
        }

    } else if (MEDIA_ID_MUSICS_BY_GENRE.equals(parentMediaId)) {
        Log.d(TAG, "OnLoadChildren.GENRES");

        List<String> genres = mMusicProvider.getGenres();
        toIndex = page == -1 ? genres.size() : Math.min(fromIndex + pageSize, genres.size());

        for (int i = fromIndex; i < toIndex; i++) {
            String genre = genres.get(i);
            MediaItem item = new MediaItem(new MediaDescriptionCompat.Builder()
                    .setMediaId(createBrowseCategoryMediaID(MEDIA_ID_MUSICS_BY_GENRE, genre)).setTitle(genre)
                    .setSubtitle(getString(R.string.browse_musics_by_genre_subtitle, genre)).build(),
                    MediaItem.FLAG_BROWSABLE);
            mediaItems.add(item);
        }

    } else if (parentMediaId.startsWith(MEDIA_ID_MUSICS_BY_GENRE)) {
        String genre = MediaIDHelper.getHierarchy(parentMediaId)[1];
        Log.d(TAG, "OnLoadChildren.SONGS_BY_GENRE  genre=" + genre);

        List<MediaMetadataCompat> tracks = mMusicProvider.getMusicsByGenre(genre);
        toIndex = page == -1 ? tracks.size() : Math.min(fromIndex + pageSize, tracks.size());

        for (int i = fromIndex; i < toIndex; i++) {
            MediaMetadataCompat track = tracks.get(i);

            // Since mediaMetadata fields are immutable, we need to create a copy, so we
            // can set a hierarchy-aware mediaID. We will need to know the media hierarchy
            // when we get a onPlayFromMusicID call, so we can create the proper queue based
            // on where the music was selected from (by artist, by genre, random, etc)
            String hierarchyAwareMediaID = MediaIDHelper.createMediaID(track.getDescription().getMediaId(),
                    MEDIA_ID_MUSICS_BY_GENRE, genre);
            MediaMetadataCompat trackCopy = new MediaMetadataCompat.Builder(track)
                    .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, hierarchyAwareMediaID).build();
            MediaItem bItem = new MediaItem(trackCopy.getDescription(), MediaItem.FLAG_PLAYABLE);
            mediaItems.add(bItem);
        }
    } else {
        Log.w(TAG, "Skipping unmatched parentMediaId: " + parentMediaId);
    }
    Log.d(TAG, "OnLoadChildren sending " + mediaItems.size() + " results for " + parentMediaId);
    result.sendResult(mediaItems);
}

From source file:com.example.android.mediabrowserservice.MusicService.java

private void updateMetadata() {
    if (!QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
        LogHelper.e(TAG, "Can't retrieve current metadata.");
        updatePlaybackState(getResources().getString(R.string.error_no_metadata));
        return;/*from   ww w . j a va  2  s .  co  m*/
    }
    MediaSessionCompat.QueueItem queueItem = mPlayingQueue.get(mCurrentIndexOnQueue);
    String musicId = MediaIDHelper.extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());
    MediaMetadataCompat track = mMusicProvider.getMusic(musicId);
    final String trackId = track.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID);
    if (!musicId.equals(trackId)) {
        IllegalStateException e = new IllegalStateException("track ID should match musicId.");
        LogHelper.e(TAG, "track ID should match musicId.", " musicId=", musicId, " trackId=", trackId,
                " mediaId from queueItem=", queueItem.getDescription().getMediaId(), " title from queueItem=",
                queueItem.getDescription().getTitle(), " mediaId from track=",
                track.getDescription().getMediaId(), " title from track=", track.getDescription().getTitle(),
                " source.hashcode from track=",
                track.getString(MusicProvider.CUSTOM_METADATA_TRACK_SOURCE).hashCode(), e);
        throw e;
    }
    LogHelper.d(TAG, "Updating metadata for MusicID= " + musicId);
    mSession.setMetadata(track);

    // Set the proper album artwork on the media session, so it can be shown in the
    // locked screen and in other places.
    if (track.getDescription().getIconBitmap() == null && track.getDescription().getIconUri() != null) {
        String albumUri = track.getDescription().getIconUri().toString();
        AlbumArtCache.getInstance().fetch(albumUri, new AlbumArtCache.FetchListener() {
            @Override
            public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                MediaSessionCompat.QueueItem queueItem = mPlayingQueue.get(mCurrentIndexOnQueue);
                MediaMetadataCompat track = mMusicProvider.getMusic(trackId);
                track = new MediaMetadataCompat.Builder(track)

                        // set high resolution bitmap in METADATA_KEY_ALBUM_ART. This is used, for
                        // example, on the lockscreen background when the media session is active.
                        .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap)

                        // set small version of the album art in the DISPLAY_ICON. This is used on
                        // the MediaDescription and thus it should be small to be serialized if
                        // necessary..
                        .putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, icon)

                        .build();

                mMusicProvider.updateMusic(trackId, track);

                // If we are still playing the same music
                String currentPlayingId = MediaIDHelper
                        .extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());
                if (trackId.equals(currentPlayingId)) {
                    mSession.setMetadata(track);
                }
            }
        });
    }
}

From source file:com.example.android.supportv4.media.MediaBrowserServiceSupport.java

private void updateMetadata() {
    if (!QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
        Log.e(TAG, "Can't retrieve current metadata.");
        updatePlaybackState(getResources().getString(R.string.error_no_metadata));
        return;/*from   w  ww .  j av  a  2  s . co m*/
    }
    MediaSessionCompat.QueueItem queueItem = mPlayingQueue.get(mCurrentIndexOnQueue);
    String musicId = MediaIDHelper.extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());
    MediaMetadataCompat track = mMusicProvider.getMusic(musicId);
    final String trackId = track.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID);
    if (!musicId.equals(trackId)) {
        IllegalStateException e = new IllegalStateException("track ID should match musicId.");
        Log.e(TAG,
                "track ID should match musicId. musicId=" + musicId + " trackId=" + trackId
                        + " mediaId from queueItem=" + queueItem.getDescription().getMediaId()
                        + " title from queueItem=" + queueItem.getDescription().getTitle()
                        + " mediaId from track=" + track.getDescription().getMediaId() + " title from track="
                        + track.getDescription().getTitle() + " source.hashcode from track="
                        + track.getString(MusicProvider.CUSTOM_METADATA_TRACK_SOURCE).hashCode(),
                e);
        throw e;
    }
    Log.d(TAG, "Updating metadata for MusicID= " + musicId);
    mSession.setMetadata(track);

    // Set the proper album artwork on the media session, so it can be shown in the
    // locked screen and in other places.
    if (track.getDescription().getIconBitmap() == null && track.getDescription().getIconUri() != null) {
        String albumUri = track.getDescription().getIconUri().toString();
        AlbumArtCache.getInstance().fetch(albumUri, new AlbumArtCache.FetchListener() {
            @Override
            public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                MediaSessionCompat.QueueItem queueItem = mPlayingQueue.get(mCurrentIndexOnQueue);
                MediaMetadataCompat track = mMusicProvider.getMusic(trackId);
                track = new MediaMetadataCompat.Builder(track)
                        // set high resolution bitmap in METADATA_KEY_ALBUM_ART. This is used,
                        // for example, on the lockscreen background when the media session is
                        // active.
                        .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap)
                        // set small version of the album art in the DISPLAY_ICON. This is used
                        // on the MediaDescription and thus it should be small to be serialized
                        // if necessary.
                        .putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, icon).build();

                mMusicProvider.updateMusic(trackId, track);

                // If we are still playing the same music
                String currentPlayingId = MediaIDHelper
                        .extractMusicIDFromMediaID(queueItem.getDescription().getMediaId());
                if (trackId.equals(currentPlayingId)) {
                    mSession.setMetadata(track);
                }
            }
        });
    }
}

From source file:com.torrenttunes.android.MusicService.java

/**
 * Actual implementation of onLoadChildren that assumes that MusicProvider is already
 * initialized./* w  w  w .ja v a 2  s  . c om*/
 */
private void loadChildrenImpl(final String parentMediaId,
        final Result<List<MediaBrowserCompat.MediaItem>> result) {
    LogHelper.d(TAG, "OnLoadChildren: parentMediaId=", parentMediaId);

    List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();

    if (MEDIA_ID_ROOT.equals(parentMediaId)) {
        LogHelper.d(TAG, "OnLoadChildren.ROOT");
        mediaItems
                .add(new MediaBrowserCompat.MediaItem(
                        new MediaDescriptionCompat.Builder().setMediaId(MEDIA_ID_MUSICS_BY_ARTISTS)
                                .setTitle(getString(R.string.browse_artists))
                                .setIconUri(Uri.parse("android.resource://"
                                        + "com.example.android.uamp/drawable/ic_by_genre"))
                                .setSubtitle(getString(R.string.browse_artists_subtitle)).build(),
                        MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));

    } else if (MEDIA_ID_MUSICS_BY_ARTISTS.equals(parentMediaId)) {
        LogHelper.d(TAG, "OnLoadChildren.ARTISTS");
        for (String artist : mMusicProvider.getArtists()) {
            MediaBrowserCompat.MediaItem item = new MediaBrowserCompat.MediaItem(
                    new MediaDescriptionCompat.Builder()
                            .setMediaId(createBrowseCategoryMediaID(MEDIA_ID_MUSICS_BY_ARTISTS, artist))
                            .setTitle(artist)
                            .setSubtitle(getString(R.string.browse_musics_by_artist_subtitle, artist)).build(),
                    MediaBrowserCompat.MediaItem.FLAG_BROWSABLE);
            mediaItems.add(item);
        }

    } else if (parentMediaId.startsWith(MEDIA_ID_MUSICS_BY_ARTISTS)) {
        String artist = MediaIDHelper.getHierarchy(parentMediaId)[1];
        LogHelper.d(TAG, "OnLoadChildren.SONGS_BY_GENRE  genre=", artist);
        for (MediaMetadataCompat track : mMusicProvider.getMusicsByArtist(artist)) {
            // Since mediaMetadata fields are immutable, we need to create a copy, so we
            // can set a hierarchy-aware mediaID. We will need to know the media hierarchy
            // when we get a onPlayFromMusicID call, so we can create the proper queue based
            // on where the music was selected from (by artist, by genre, random, etc)
            String hierarchyAwareMediaID = MediaIDHelper.createMediaID(track.getDescription().getMediaId(),
                    MEDIA_ID_MUSICS_BY_ARTISTS, artist);
            MediaMetadataCompat trackCopy = new MediaMetadataCompat.Builder(track)
                    .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, hierarchyAwareMediaID).build();
            MediaBrowserCompat.MediaItem bItem = new MediaBrowserCompat.MediaItem(trackCopy.getDescription(),
                    MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);
            mediaItems.add(bItem);
        }
    } else {
        LogHelper.w(TAG, "Skipping unmatched parentMediaId: ", parentMediaId);
    }
    LogHelper.d(TAG, "OnLoadChildren sending ", mediaItems.size(), " results for ", parentMediaId);
    result.sendResult(mediaItems);
}

From source file:android.support.v7.app.MediaRouteControllerDialog.java

/**
 * Set the session to use for metadata and transport controls. The dialog
 * will listen to changes on this session and update the UI automatically in
 * response to changes./*from   w  w  w.j  a  v  a 2  s .  co m*/
 *
 * @param sessionToken The token for the session to use.
 */
private void setMediaSession(MediaSessionCompat.Token sessionToken) {
    if (mMediaController != null) {
        mMediaController.unregisterCallback(mControllerCallback);
        mMediaController = null;
    }
    if (sessionToken == null) {
        return;
    }
    if (!mAttachedToWindow) {
        return;
    }
    try {
        mMediaController = new MediaControllerCompat(mContext, sessionToken);
    } catch (RemoteException e) {
        Log.e(TAG, "Error creating media controller in setMediaSession.", e);
    }
    if (mMediaController != null) {
        mMediaController.registerCallback(mControllerCallback);
    }
    MediaMetadataCompat metadata = mMediaController == null ? null : mMediaController.getMetadata();
    mDescription = metadata == null ? null : metadata.getDescription();
    mState = mMediaController == null ? null : mMediaController.getPlaybackState();
    update(false);
}

From source file:androidx.mediarouter.app.MediaRouteControllerDialog.java

/**
 * Set the session to use for metadata and transport controls. The dialog
 * will listen to changes on this session and update the UI automatically in
 * response to changes.//from  w  ww .  java  2 s. c o m
 *
 * @param sessionToken The token for the session to use.
 */
private void setMediaSession(MediaSessionCompat.Token sessionToken) {
    if (mMediaController != null) {
        mMediaController.unregisterCallback(mControllerCallback);
        mMediaController = null;
    }
    if (sessionToken == null) {
        return;
    }
    if (!mAttachedToWindow) {
        return;
    }
    try {
        mMediaController = new MediaControllerCompat(mContext, sessionToken);
    } catch (RemoteException e) {
        Log.e(TAG, "Error creating media controller in setMediaSession.", e);
    }
    if (mMediaController != null) {
        mMediaController.registerCallback(mControllerCallback);
    }
    MediaMetadataCompat metadata = mMediaController == null ? null : mMediaController.getMetadata();
    mDescription = metadata == null ? null : metadata.getDescription();
    mState = mMediaController == null ? null : mMediaController.getPlaybackState();
    updateArtIconIfNeeded();
    update(false);
}