android.support.v4.media.MediaDescriptionCompat

Here are the examples of the java api class android.support.v4.media.MediaDescriptionCompat taken from open source projects.

1. TomahawkMediaBrowserService#onLoadChildren()

Project: tomahawk-android
File: TomahawkMediaBrowserService.java
@Override
public void onLoadChildren(@NonNull final String parentMediaId, @NonNull final Result<List<MediaBrowserCompat.MediaItem>> result) {
    Log.d(TAG, "OnLoadChildren.ROOT");
    List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();
    MediaDescriptionCompat description = new MediaDescriptionCompat.Builder().setMediaId("__ALBUMS__").setTitle("test").build();
    mediaItems.add(new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE));
    result.sendResult(mediaItems);
}

2. MediaNotification#updateNotificationMetadata()

Project: tomahawk-android
File: MediaNotification.java
private void updateNotificationMetadata() {
    Log.d(TAG, "updateNotificationMetadata. mMetadata=" + mMetadata);
    if (mMetadata == null || mPlaybackState == null) {
        return;
    }
    mNotificationBuilder = new NotificationCompat.Builder(mService);
    List<Integer> showInCompact = new ArrayList<>();
    updateFavoriteAction();
    showInCompact.add(mNotificationBuilder.mActions.size());
    mNotificationBuilder.addAction(mFavoriteAction);
    // If skip to previous action is enabled
    if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
        NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_player_previous_light, mService.getString(R.string.playback_previous), mIntents.get(R.drawable.ic_player_previous_light)).build();
        mNotificationBuilder.addAction(action);
    }
    updatePlayPauseAction();
    showInCompact.add(mNotificationBuilder.mActions.size());
    mNotificationBuilder.addAction(mPlayPauseAction);
    // If skip to next action is enabled
    if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
        NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_player_next_light, mService.getString(R.string.playback_next), mIntents.get(R.drawable.ic_player_next_light)).build();
        showInCompact.add(mNotificationBuilder.mActions.size());
        mNotificationBuilder.addAction(action);
    }
    MediaDescriptionCompat description = mMetadata.getDescription();
    Bitmap art = description.getIconBitmap();
    if (art == null) {
        // use a placeholder art while the remote art is being downloaded
        art = BitmapFactory.decodeResource(mService.getResources(), R.drawable.album_placeholder);
    }
    mNotificationBuilder.setStyle(new NotificationCompat.MediaStyle().setShowActionsInCompactView(ArrayUtil.toIntArray(showInCompact)).setMediaSession(mSessionToken).setShowCancelButton(true)).setColor(mNotificationColor).setSmallIcon(R.drawable.ic_notification).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setContentIntent(createContentIntent()).setContentTitle(description.getTitle()).setContentText(description.getSubtitle()).setTicker(description.getTitle() + " - " + description.getSubtitle()).setLargeIcon(art).setOngoing(false);
    updateNotificationPlaybackState();
    mService.startForeground(NOTIFICATION_ID, mNotificationBuilder.build());
    Log.d(TAG, "updateNotificationMetadata. Notification shown");
}

3. MediaNotificationManager#createNotification()

Project: LyricHere
File: MediaNotificationManager.java
private Notification createNotification() {
    LogUtils.d(TAG, "updateNotificationMetadata. mMetadata=" + mMetadata);
    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService);
    int playPauseButtonPosition = 0;
    // If skip to previous action is enabled
    if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
        notificationBuilder.addAction(R.drawable.ic_skip_previous_white_24dp, mService.getString(R.string.label_previous), mPreviousIntent);
        // If there is a "skip to previous" button, the play/pause button will
        // be the second one. We need to keep track of it, because the MediaStyle notification
        // requires to specify the index of the buttons (actions) that should be visible
        // when in compact view.
        playPauseButtonPosition = 1;
    }
    addPlayPauseAction(notificationBuilder);
    // If skip to next action is enabled
    if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
        notificationBuilder.addAction(R.drawable.ic_skip_next_white_24dp, mService.getString(R.string.label_next), mNextIntent);
    }
    MediaDescriptionCompat description = mMetadata.getDescription();
    String fetchArtUrl = null;
    Bitmap art = null;
    if (description.getIconUri() != null) {
        // This sample assumes the iconUri will be a valid URL formatted String, but
        // it can actually be any valid Android Uri formatted String.
        // async fetch the album art icon
        String artUrl = description.getIconUri().toString();
        art = AlbumArtCache.getInstance().getBigImage(artUrl);
        if (art == null || art.isRecycled()) {
            fetchArtUrl = artUrl;
            // use a placeholder art while the remote art is being downloaded
            art = BitmapFactory.decodeResource(mService.getResources(), R.drawable.ic_default_art);
        }
    }
    notificationBuilder.setStyle(new NotificationCompat.MediaStyle().setShowActionsInCompactView(// show only play/pause in compact view
    new int[] { playPauseButtonPosition }).setMediaSession(mSessionToken)).setColor(mNotificationColor).setSmallIcon(R.drawable.ic_notification).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setUsesChronometer(true).setContentIntent(createContentIntent(description)).setContentTitle(description.getTitle()).setContentText(description.getSubtitle()).setLargeIcon(art);
    if (mController != null && mController.getExtras() != null) {
        String castName = mController.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
        if (castName != null) {
            String castInfo = mService.getResources().getString(R.string.casting_to_device, castName);
            notificationBuilder.setSubText(castInfo);
            notificationBuilder.addAction(R.drawable.ic_close_black_24dp, mService.getString(R.string.stop_casting), mStopCastIntent);
        }
    }
    setNotificationPlaybackState(notificationBuilder);
    if (fetchArtUrl != null) {
        fetchBitmapFromURLAsync(fetchArtUrl, notificationBuilder);
    }
    return notificationBuilder.build();
}

4. PlaybackService#createBrowsableMediaItemForRoot()

Project: AntennaPod
File: PlaybackService.java
private MediaBrowserCompat.MediaItem createBrowsableMediaItemForRoot() {
    MediaDescriptionCompat description = new MediaDescriptionCompat.Builder().setMediaId(getResources().getString(R.string.queue_label)).setTitle(getResources().getString(R.string.queue_label)).build();
    return new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE);
}

5. FeedMedia#getMediaItem()

Project: AntennaPod
File: FeedMedia.java
/**
     * Returns a MediaItem representing the FeedMedia object.
     * This is used by the MediaBrowserService
     */
public MediaBrowserCompat.MediaItem getMediaItem() {
    Playable p = this;
    MediaDescriptionCompat description = new MediaDescriptionCompat.Builder().setMediaId(String.valueOf(id)).setTitle(p.getEpisodeTitle()).setDescription(p.getFeedTitle()).setSubtitle(p.getFeedTitle()).build();
    return new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);
}

6. CustomMRControllerDialog#updateViews()

Project: AntennaPod
File: CustomMRControllerDialog.java
private void updateViews() {
    if (!viewsCreated || token == null || mediaController == null) {
        rootView.setVisibility(View.GONE);
        return;
    }
    MediaMetadataCompat metadata = mediaController.getMetadata();
    MediaDescriptionCompat description = metadata == null ? null : metadata.getDescription();
    if (description == null) {
        rootView.setVisibility(View.GONE);
        return;
    }
    PlaybackStateCompat state = mediaController.getPlaybackState();
    MediaRouter.RouteInfo route = MediaRouter.getInstance(getContext()).getSelectedRoute();
    CharSequence title = description.getTitle();
    boolean hasTitle = !TextUtils.isEmpty(title);
    CharSequence subtitle = description.getSubtitle();
    boolean hasSubtitle = !TextUtils.isEmpty(subtitle);
    boolean showTitle = false;
    boolean showSubtitle = false;
    if (route.getPresentationDisplayId() != MediaRouter.RouteInfo.PRESENTATION_DISPLAY_ID_NONE) {
        // The user is currently casting screen.
        titleView.setText(android.support.v7.mediarouter.R.string.mr_controller_casting_screen);
        showTitle = true;
    } else if (state == null || state.getState() == PlaybackStateCompat.STATE_NONE) {
        // (Only exception is bluetooth where we don't show anything.)
        if (!route.isDeviceTypeBluetooth()) {
            titleView.setText(android.support.v7.mediarouter.R.string.mr_controller_no_media_selected);
            showTitle = true;
        }
    } else if (!hasTitle && !hasSubtitle) {
        titleView.setText(android.support.v7.mediarouter.R.string.mr_controller_no_info_available);
        showTitle = true;
    } else {
        if (hasTitle) {
            titleView.setText(title);
            showTitle = true;
        }
        if (hasSubtitle) {
            subtitleView.setText(subtitle);
            showSubtitle = true;
        }
    }
    if (showSubtitle) {
        titleView.setSingleLine();
    } else {
        titleView.setMaxLines(2);
    }
    titleView.setVisibility(showTitle ? View.VISIBLE : View.GONE);
    subtitleView.setVisibility(showSubtitle ? View.VISIBLE : View.GONE);
    updateState();
    if (rootView.getVisibility() != View.VISIBLE) {
        artView.setVisibility(View.GONE);
        rootView.setVisibility(View.VISIBLE);
    }
    if (fetchArtSubscription != null) {
        fetchArtSubscription.unsubscribe();
    }
    fetchArtSubscription = Observable.fromCallable(() -> fetchArt(description)).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe( result -> {
        fetchArtSubscription = null;
        if (result.first != null) {
            artView.setBackgroundColor(result.second);
            artView.setImageBitmap(result.first);
            artView.setVisibility(View.VISIBLE);
        } else {
            artView.setVisibility(View.GONE);
        }
    },  error -> Log.e(TAG, Log.getStackTraceString(error)));
}

7. CardPresenter#onBindViewHolder()

Project: android-UniversalMusicPlayer
File: CardPresenter.java
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
    MediaDescriptionCompat description;
    if (item instanceof MediaBrowserCompat.MediaItem) {
        MediaBrowserCompat.MediaItem mediaItem = (MediaBrowserCompat.MediaItem) item;
        LogHelper.d(TAG, "onBindViewHolder MediaItem: ", mediaItem.toString());
        description = mediaItem.getDescription();
    } else if (item instanceof MediaSessionCompat.QueueItem) {
        MediaSessionCompat.QueueItem queueItem = (MediaSessionCompat.QueueItem) item;
        description = queueItem.getDescription();
    } else {
        throw new IllegalArgumentException("Object must be MediaItem or QueueItem, not " + item.getClass().getSimpleName());
    }
    final CardViewHolder cardViewHolder = (CardViewHolder) viewHolder;
    cardViewHolder.mCardView.setTitleText(description.getTitle());
    cardViewHolder.mCardView.setContentText(description.getSubtitle());
    cardViewHolder.mCardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);
    Uri artUri = description.getIconUri();
    if (artUri == null) {
        setCardImage(cardViewHolder, description.getIconBitmap());
    } else {
        // IconUri potentially has a better resolution than iconBitmap.
        String artUrl = artUri.toString();
        AlbumArtCache cache = AlbumArtCache.getInstance();
        if (cache.getBigImage(artUrl) != null) {
            // So, we use it immediately if it's cached:
            setCardImage(cardViewHolder, cache.getBigImage(artUrl));
        } else {
            // Otherwise, we use iconBitmap if available while we wait for iconURI
            setCardImage(cardViewHolder, description.getIconBitmap());
            cache.fetch(artUrl, new AlbumArtCache.FetchListener() {

                @Override
                public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
                    setCardImage(cardViewHolder, bitmap);
                }
            });
        }
    }
}

8. MusicProvider#createBrowsableMediaItemForGenre()

Project: android-UniversalMusicPlayer
File: MusicProvider.java
private MediaBrowserCompat.MediaItem createBrowsableMediaItemForGenre(String genre, Resources resources) {
    MediaDescriptionCompat description = new MediaDescriptionCompat.Builder().setMediaId(createMediaID(null, MEDIA_ID_MUSICS_BY_GENRE, genre)).setTitle(genre).setSubtitle(resources.getString(R.string.browse_musics_by_genre_subtitle, genre)).build();
    return new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE);
}

9. MusicProvider#createBrowsableMediaItemForRoot()

Project: android-UniversalMusicPlayer
File: MusicProvider.java
private MediaBrowserCompat.MediaItem createBrowsableMediaItemForRoot(Resources resources) {
    MediaDescriptionCompat description = new MediaDescriptionCompat.Builder().setMediaId(MEDIA_ID_MUSICS_BY_GENRE).setTitle(resources.getString(R.string.browse_genres)).setSubtitle(resources.getString(R.string.browse_genre_subtitle)).setIconUri(Uri.parse("android.resource://" + "com.example.android.uamp/drawable/ic_by_genre")).build();
    return new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE);
}

10. MediaNotificationManager#createNotification()

Project: android-UniversalMusicPlayer
File: MediaNotificationManager.java
private Notification createNotification() {
    LogHelper.d(TAG, "updateNotificationMetadata. mMetadata=" + mMetadata);
    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService);
    int playPauseButtonPosition = 0;
    // If skip to previous action is enabled
    if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
        notificationBuilder.addAction(R.drawable.ic_skip_previous_white_24dp, mService.getString(R.string.label_previous), mPreviousIntent);
        // If there is a "skip to previous" button, the play/pause button will
        // be the second one. We need to keep track of it, because the MediaStyle notification
        // requires to specify the index of the buttons (actions) that should be visible
        // when in compact view.
        playPauseButtonPosition = 1;
    }
    addPlayPauseAction(notificationBuilder);
    // If skip to next action is enabled
    if ((mPlaybackState.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
        notificationBuilder.addAction(R.drawable.ic_skip_next_white_24dp, mService.getString(R.string.label_next), mNextIntent);
    }
    MediaDescriptionCompat description = mMetadata.getDescription();
    String fetchArtUrl = null;
    Bitmap art = null;
    if (description.getIconUri() != null) {
        // This sample assumes the iconUri will be a valid URL formatted String, but
        // it can actually be any valid Android Uri formatted String.
        // async fetch the album art icon
        String artUrl = description.getIconUri().toString();
        art = AlbumArtCache.getInstance().getBigImage(artUrl);
        if (art == null) {
            fetchArtUrl = artUrl;
            // use a placeholder art while the remote art is being downloaded
            art = BitmapFactory.decodeResource(mService.getResources(), R.drawable.ic_default_art);
        }
    }
    notificationBuilder.setStyle(new NotificationCompat.MediaStyle().setShowActionsInCompactView(// show only play/pause in compact view
    new int[] { playPauseButtonPosition }).setMediaSession(mSessionToken)).setColor(mNotificationColor).setSmallIcon(R.drawable.ic_notification).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setUsesChronometer(true).setContentIntent(createContentIntent(description)).setContentTitle(description.getTitle()).setContentText(description.getSubtitle()).setLargeIcon(art);
    if (mController != null && mController.getExtras() != null) {
        String castName = mController.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
        if (castName != null) {
            String castInfo = mService.getResources().getString(R.string.casting_to_device, castName);
            notificationBuilder.setSubText(castInfo);
            notificationBuilder.addAction(R.drawable.ic_close_black_24dp, mService.getString(R.string.stop_casting), mStopCastIntent);
        }
    }
    setNotificationPlaybackState(notificationBuilder);
    if (fetchArtUrl != null) {
        fetchBitmapFromURLAsync(fetchArtUrl, notificationBuilder);
    }
    return notificationBuilder.build();
}