Java Code Examples for android.support.v4.media.session.MediaSessionCompat#getController()

The following examples show how to use android.support.v4.media.session.MediaSessionCompat#getController() . 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: 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 3
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 4
Source File: PlaybackFragment.java    From leanback-assistant with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int movieId = getActivity().getIntent().getIntExtra(EXTRA_MOVIE_ID, -1);
    if (movieId == -1) {
        Log.w(TAG, "Invalid movieId, cannot playback.");
        throw new IllegalArgumentException("Invalid movieId " + movieId);
    }

    mPlaylistAdapter =
            MockPlaylistAdapterFactory.createMoviePlaylistAdapterWithActiveMovieId(movieId);

    mSession = new MediaSessionCompat(getContext(), TAG);
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
                    | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mSession.setActive(true);
    MediaControllerCompat.setMediaController((Activity) getContext(), mSession.getController());

    mPlayerGlue =
            new PrimaryPlaybackControlsGlue<>(
                    getContext(),
                    new MediaPlayerAdapter(getContext()),
                    mSession.getController());
    mPlayerGlue.setHost(new VideoSupportFragmentGlueHost(this));
    mPlayerGlue.addPlayerCallback(playWhenReadyPlayerCallback);
    mPlayerGlue.addPlayerCallback(playPausePlayerCallback);

    mMediaSessionCallback = new MediaSessionCallback(mPlayerGlue);
    mSession.setCallback(mMediaSessionCallback);

    playMedia(mPlaylistAdapter.getCurrentItem());
}
 
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: 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 8
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 9
Source File: SimpleAgent.java    From BeMusic with Apache License 2.0 4 votes vote down vote up
private NotificationCompat.Builder getBuilderCompat (Context context, PlayManager manager, @PlayService.State int state, Song song) {

        PendingIntent playPauseIt = getActionIntent(context, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
        PendingIntent previousIt = getActionIntent(context, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
        PendingIntent nextIt = getActionIntent(context, KeyEvent.KEYCODE_MEDIA_NEXT);
        PendingIntent deleteIntent = getActionIntent(context, KeyEvent.KEYCODE_MEDIA_STOP);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, PlayDetailActivity.class), 0);

        boolean isPlaying = manager.isPlaying();
        MediaSessionCompat sessionCompat = manager.getMediaSessionCompat();
        MediaControllerCompat controllerCompat = sessionCompat.getController();
        PendingIntent sessionActivity = controllerCompat.getSessionActivity();
        Log.v(TAG, "getBuilderCompat sessionActivity = " + sessionActivity);
        MediaDescriptionCompat descriptionCompat = controllerCompat.getMetadata().getDescription();

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentTitle(descriptionCompat.getTitle());
        builder.setShowWhen(false);
        builder.setContentText(descriptionCompat.getSubtitle());
        builder.setSubText(descriptionCompat.getDescription());
        builder.setSmallIcon(R.drawable.ic_notification_queue_music);
        builder.addAction(R.drawable.ic_skip_previous, "previous", previousIt);
        builder.addAction(isPlaying ? R.drawable.ic_pause : R.drawable.ic_play, "play pause", playPauseIt);
        builder.addAction(R.drawable.ic_skip_next, "next", nextIt);
        builder.setContentIntent(contentIntent);
        builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        builder.setLargeIcon(descriptionCompat.getIconBitmap());
        NotificationCompat.MediaStyle mediaStyle = new NotificationCompat.MediaStyle();
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
            builder.setOngoing(true);
            mediaStyle.setShowCancelButton(true);
            mediaStyle.setCancelButtonIntent(deleteIntent);
        } else {
            builder.setOngoing(isPlaying);
            builder.setDeleteIntent(deleteIntent);
        }
        mediaStyle.setShowActionsInCompactView(0, 1, 2);

        if (sessionCompat != null) {
            mediaStyle.setMediaSession(sessionCompat.getSessionToken());
        }
        builder.setStyle(mediaStyle);
        return builder;
    }