Java Code Examples for android.support.v4.media.MediaMetadataCompat#getDescription()

The following examples show how to use android.support.v4.media.MediaMetadataCompat#getDescription() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MediaStyleHelper.java    From AndroidAudioExample with MIT License 6 votes vote down vote up
/**
 * Build a notification using the information from the given media session. Makes heavy use
 * of {@link MediaMetadataCompat#getDescription()} to extract the appropriate information.
 *
 * @param context      Context used to construct the notification.
 * @param mediaSession Media session to get information.
 * @return A pre-built notification with information from the given media session.
 */
static NotificationCompat.Builder from(Context context, MediaSessionCompat mediaSession) {
    MediaControllerCompat controller = mediaSession.getController();
    MediaMetadataCompat mediaMetadata = controller.getMetadata();
    MediaDescriptionCompat description = mediaMetadata.getDescription();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setSubText(description.getDescription())
            .setLargeIcon(description.getIconBitmap())
            .setContentIntent(controller.getSessionActivity())
            .setDeleteIntent(
                    MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_STOP))
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    return builder;
}
 
Example 2
Source File: QueueHelper.java    From LyricHere with Apache License 2.0 6 votes vote down vote up
private static List<MediaSessionCompat.QueueItem> convertToQueue(
        Iterable<MediaMetadataCompat> tracks, String... categories) {
    List<MediaSessionCompat.QueueItem> queue = new ArrayList<>();
    int count = 0;
    for (MediaMetadataCompat track : tracks) {

        // We create a hierarchy-aware mediaID, so we know what the queue is about by looking
        // at the QueueItem media IDs.
        String hierarchyAwareMediaID = MediaIDHelper.createMediaID(
                track.getDescription().getMediaId(), categories);

        MediaMetadataCompat trackCopy = new MediaMetadataCompat.Builder(track)
                .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, hierarchyAwareMediaID)
                .build();

        // We don't expect queues to change after created, so we use the item index as the
        // queueId. Any other number unique in the queue would work.
        MediaSessionCompat.QueueItem item = new MediaSessionCompat.QueueItem(trackCopy.getDescription(), count++);
        queue.add(item);
    }
    return queue;

}
 
Example 3
Source File: NotificationUtil.java    From Prodigal with Apache License 2.0 6 votes vote down vote up
private NotificationCompat.Builder notificationBuilder(PlayerService context, MediaSessionCompat session) {
    MediaControllerCompat controller = session.getController();
    MediaMetadataCompat mediaMetadata = controller.getMetadata();
    MediaDescriptionCompat description = mediaMetadata.getDescription();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setSubText(description.getDescription())
            .setLargeIcon(mediaMetadata.getBitmap(MediaMetadataCompat.METADATA_KEY_ART))
            .setContentIntent(clickIntent)
            .setDeleteIntent(
                    MediaButtonReceiver.buildMediaButtonPendingIntent(context, receiverName, PlaybackStateCompat.ACTION_STOP))
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    return builder;
}
 
Example 4
Source File: TtsService.java    From android-app with GNU General Public License v3.0 6 votes vote down vote up
private NotificationCompat.Builder generateNotificationBuilderFrom(
        Context context, MediaSessionCompat mediaSession) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context, NotificationsHelper.CHANNEL_ID_TTS)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setDeleteIntent(generateActionIntent(PlaybackStateCompat.ACTION_STOP));

    if (mediaSession != null) {
        MediaControllerCompat controller = mediaSession.getController();

        builder.setContentIntent(controller.getSessionActivity());

        MediaMetadataCompat mediaMetadata = controller.getMetadata();
        if (mediaMetadata != null) {
            MediaDescriptionCompat description = mediaMetadata.getDescription();

            builder.setContentTitle(description.getTitle())
                    .setContentText(description.getSubtitle())
                    .setSubText(description.getDescription())
                    .setLargeIcon(description.getIconBitmap());
        }
    }

    return builder;
}
 
Example 5
Source File: MediaStyleHelper.java    From Jockey with Apache License 2.0 5 votes vote down vote up
/**
 * Build a notification using the information from the given media session.
 * @param context Context used to construct the notification.
 * @param mediaSession Media session to get information.
 * @return A pre-built notification with information from the given media session.
 */
public static NotificationCompat.Builder from(Context context,
                                              MediaSessionCompat mediaSession,
                                              String channel) {

    MediaControllerCompat controller = mediaSession.getController();
    MediaMetadataCompat mediaMetadata = controller.getMetadata();
    MediaDescriptionCompat description = mediaMetadata.getDescription();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channel);

    builder
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setSubText(description.getDescription())
            .setContentIntent(controller.getSessionActivity())
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setWhen(0)
            .setShowWhen(false);

    if (description.getIconBitmap() == null || description.getIconBitmap().isRecycled()) {
        builder.setLargeIcon(
                BitmapFactory.decodeResource(context.getResources(), R.drawable.art_default));
    } else {
        builder.setLargeIcon(description.getIconBitmap());
    }

    return builder;
}
 
Example 6
Source File: MediaStyleHelper.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Build a notification using the information from the given media session. Makes heavy use
 * of {@link MediaMetadataCompat#getDescription()} to extract the appropriate information.
 * @param context Context used to construct the notification.
 * @param mediaSession Media session to get information.
 * @return A pre-built notification with information from the given media session.
 */
public static NotificationCompat.Builder from(
        Context context, MediaSessionCompat mediaSession) {
    MediaControllerCompat controller = mediaSession.getController();
    if (controller == null) {
        return null;
    }
    MediaMetadataCompat mediaMetadata = controller.getMetadata();
    if (mediaMetadata == null) {
        return null;

    }

    MediaDescriptionCompat description = mediaMetadata.getDescription();
    if (description == null) {
        return null;
    }

 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, BackgroundAudioService.CHANNEL_ID);
    builder
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setSubText(description.getDescription())
            .setLargeIcon(description.getIconBitmap())
            .setContentIntent(controller.getSessionActivity())

            .setDeleteIntent(
                    MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_STOP))
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    return builder;
}
 
Example 7
Source File: MediaNotificationManager.java    From android-MediaBrowserService with Apache License 2.0 5 votes vote down vote up
public Notification getNotification(MediaMetadataCompat metadata,
                                    @NonNull PlaybackStateCompat state,
                                    MediaSessionCompat.Token token) {
    boolean isPlaying = state.getState() == PlaybackStateCompat.STATE_PLAYING;
    MediaDescriptionCompat description = metadata.getDescription();
    NotificationCompat.Builder builder =
            buildNotification(state, token, isPlaying, description);
    return builder.build();
}
 
Example 8
Source File: NotificationWrapper.java    From react-native-jw-media-player with MIT License 4 votes vote down vote up
public Notification createNotification(Context context,
                                          MediaSessionCompat mediaSession,
                                          long capabilities
) {
	int appIcon = context.getResources().getIdentifier("ic_app_icon", "drawable", context.getPackageName());

	// Media metadata
	MediaControllerCompat controller = mediaSession.getController();
	MediaMetadataCompat mediaMetadata = controller.getMetadata();
	MediaDescriptionCompat description = mediaMetadata.getDescription();

	// Notification
	NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
																		NOTIFICATION_CHANNEL_ID);
	builder.setContentTitle(description.getTitle())
		   .setContentText(description.getSubtitle())
		   .setSubText(description.getDescription())
		   .setLargeIcon(description.getIconBitmap())
		   .setOnlyAlertOnce(true)
		   .setStyle(new MediaStyle()
				   .setMediaSession(mediaSession.getSessionToken())
				   .setShowActionsInCompactView(0))
		   .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
		   .setSmallIcon(appIcon > 0 ? appIcon : R.drawable.ic_jw_developer)
		   .setDeleteIntent(getActionIntent(context, KeyEvent.KEYCODE_MEDIA_STOP));


	// Attach actions to the notification.
	if ((capabilities & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
		builder.addAction(R.drawable.ic_previous, "Previous",
						  getActionIntent(context,
										  KeyEvent.KEYCODE_MEDIA_PREVIOUS));
	}
	if ((capabilities & PlaybackStateCompat.ACTION_PAUSE) != 0) {
		builder.addAction(R.drawable.ic_pause, "Pause",
						  getActionIntent(context,
										  KeyEvent.KEYCODE_MEDIA_PAUSE));
	}
	if ((capabilities & PlaybackStateCompat.ACTION_PLAY) != 0) {
		builder.addAction(R.drawable.ic_play, "Play",
						  getActionIntent(context,
										  KeyEvent.KEYCODE_MEDIA_PLAY)
		);
	}
	if ((capabilities & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
		builder.addAction(R.drawable.ic_next, "Next",
						  getActionIntent(context,
										  KeyEvent.KEYCODE_MEDIA_NEXT));
	}

	// We want to resume the existing VideoActivity, over creating a new one.
	Intent intent = new Intent(context, context.getClass());
	intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
	PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
	builder.setContentIntent(pendingIntent);

	Notification notification = builder.build();

	mNotificationManager.notify(NOTIFICATION_ID, notification);

	return notification;
}
 
Example 9
Source File: MediaPlaybackService.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 4 votes vote down vote up
public Notification buildMediaNotification() {
  MediaControllerCompat controller = mMediaSession.getController();
  MediaMetadataCompat mediaMetadata = controller.getMetadata();
  MediaDescriptionCompat description = mediaMetadata.getDescription();

  NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

  // Add description metadata from the media session
  builder
    .setContentTitle(description.getTitle())
    .setContentText(description.getSubtitle())
    .setSubText(description.getDescription())
    .setLargeIcon(description.getIconBitmap())
    .setContentIntent(controller.getSessionActivity())
    .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(
      this, // Context
      PlaybackStateCompat.ACTION_STOP))
    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

  // Add branding from your app
  builder
    .setSmallIcon(R.drawable.notification_icon)
    .setColor(ContextCompat.getColor(this, R.color.colorPrimary));

  // Add actions
  builder
    .addAction(new NotificationCompat.Action(
      R.drawable.pause, getString(R.string.pause),
      MediaButtonReceiver.buildMediaButtonPendingIntent(
        this, PlaybackStateCompat.ACTION_PLAY_PAUSE)))
    .addAction(new NotificationCompat.Action(
      R.drawable.skip_to_next, getString(R.string.skip_to_next),
      MediaButtonReceiver.buildMediaButtonPendingIntent(
        this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)));

  // Add the MediaStyle
  builder
    .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
    .setShowActionsInCompactView(0)
    .setMediaSession(mMediaSession.getSessionToken())
    // These two lines are only required if your minSdkVersion is <API 21
    .setShowCancelButton(true)
    .setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(
      this, PlaybackStateCompat.ACTION_STOP)));

  return builder.build();
}
 
Example 10
Source File: VinylCastHelpers.java    From vinyl-cast with MIT License 4 votes vote down vote up
public static void createStopNotification(MediaSessionCompat mediaSession,
                                          Service context,
                                          Class<?> serviceClass,
                                          String NOTIFICATION_CHANNEL_ID,
                                          int NOTIFICATION_ID,
                                          String httpStreamUrl) {
    createNotificationChannel(context, NOTIFICATION_CHANNEL_ID);

    PendingIntent stopIntent = PendingIntent.getService(context, 0, getServiceActionIntent(VinylCastService.ACTION_STOP_RECORDING, context, serviceClass), PendingIntent.FLAG_CANCEL_CURRENT);
    PendingIntent mainActivityIntent = PendingIntent.getActivity(context, 0, getActivityIntent(context, MainActivity.class), 0);

    MediaControllerCompat controller = mediaSession.getController();
    MediaMetadataCompat mediaMetadata = controller.getMetadata();
    MediaDescriptionCompat mediaDescription = mediaMetadata == null ? null : mediaMetadata.getDescription();

    CharSequence contentTitle = mediaDescription == null ? context.getString(R.string.notification_content_title) : mediaDescription.getTitle();
    CharSequence contentText = mediaDescription == null ? httpStreamUrl : mediaDescription.getSubtitle();
    CharSequence subText = mediaDescription == null ? null : mediaDescription.getDescription();
    Bitmap largeIcon = mediaDescription == null ? null : mediaDescription.getIconBitmap();

    // Start foreground service to avoid unexpected kill
    Notification notification = new Builder(context)
            .setChannelId(NOTIFICATION_CHANNEL_ID)
            .setContentTitle(contentTitle)
            .setContentText(contentText)
            .setSubText(subText)
            .setLargeIcon(largeIcon)
            .setContentIntent(mainActivityIntent)
            .setDeleteIntent(stopIntent)
            .setShowWhen(false)
            // Add a pause button
            .addAction(new Action(
                    R.drawable.ic_stop_black_24dp, context.getString(R.string.button_stop),
                    MediaButtonReceiver.buildMediaButtonPendingIntent(context,
                            PlaybackStateCompat.ACTION_STOP)))
            .setStyle(new MediaStyle()
                    .setMediaSession(mediaSession.getSessionToken())
                    .setShowActionsInCompactView(0)
                    .setShowCancelButton(true)
                    .setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(context,
                            PlaybackStateCompat.ACTION_STOP)))
            .setSmallIcon(R.drawable.ic_record_black_100dp)
            .setVisibility(VISIBILITY_PUBLIC)
            .build();

    context.startForeground(NOTIFICATION_ID, notification);
}
 
Example 11
Source File: NotificationBuilder.java    From YouTube-In-Background with MIT License 4 votes vote down vote up
public Notification buildNotification(MediaSessionCompat.Token sessionToken) throws RemoteException
{
    if (shouldCreateNowRunningChannel()) {
        createNotificationChannel();
    }
    mediaController = new MediaControllerCompat(context, sessionToken);
    MediaMetadataCompat metadata = mediaController.getMetadata();
    mediaDescription = metadata.getDescription();
    playbackState = mediaController.getPlaybackState();

    builder = new NotificationCompat.Builder(context, CHANNEL_ID);

    // Only add actions for skip back, play/pause, skip forward, based on what's enabled.
    int playPauseIndex = 0;
    if (isSkipToPreviousEnabled()) {
        builder.addAction(skipToPreviousAction);
        playPauseIndex++;
    }

    if (isPlaying()) {
        builder.addAction(pauseAction);
    } else if (isPlayEnabled()) {
        builder.addAction(playAction);
    }

    if (isSkipToNextEnabled()) {
        builder.addAction(skipToNextAction);
    }

    MediaStyle mediaStyle = new MediaStyle()
            .setCancelButtonIntent(stopPendingIntent)
            .setMediaSession(sessionToken)
            .setShowActionsInCompactView(playPauseIndex)
            .setShowCancelButton(true);

    return builder.setContentIntent(mediaController.getSessionActivity())
            .setContentText(mediaDescription.getSubtitle())
            .setContentTitle(mediaDescription.getTitle())
            .setDeleteIntent(stopPendingIntent)
            .setLargeIcon(mediaDescription.getIconBitmap())
            .setOnlyAlertOnce(true)
            .setSmallIcon(R.drawable.ic_notification)
            .setStyle(mediaStyle)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .build();
}