Example usage for android.support.v4.media.session MediaSessionCompat getSessionToken

List of usage examples for android.support.v4.media.session MediaSessionCompat getSessionToken

Introduction

In this page you can find the example usage for android.support.v4.media.session MediaSessionCompat getSessionToken.

Prototype

public Token getSessionToken() 

Source Link

Document

Retrieves a token object that can be used by apps to create a MediaControllerCompat for interacting with this session.

Usage

From source file:com.doctoror.fuckoffmusicplayer.presentation.media.browser.MediaBrowserServiceImpl.java

@Override
public void onCreate() {
    super.onCreate();
    mPackageValidator = new PackageValidator(this);
    mMediaBrowser = new MediaBrowserImpl(this);

    mMediaSessionHolder.openSession();/*from w  w  w.  j  a  va  2s .c  o m*/

    final MediaSessionCompat mediaSession = mMediaSessionHolder.getMediaSession();
    if (mediaSession != null) {
        setSessionToken(mediaSession.getSessionToken());
    }
}

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

public static Notification createNotification(Context context, MediaSessionCompat mediaSession) {
    MediaControllerCompat controller = mediaSession.getController();
    MediaMetadataCompat mMetadata = controller.getMetadata();
    PlaybackStateCompat mPlaybackState = controller.getPlaybackState();

    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }/*from  w w  w . j  a v a2 s .com*/

    boolean isPlaying = mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING;
    NotificationCompat.Action action = isPlaying
            ? new NotificationCompat.Action(R.drawable.ic_pause_white_24dp,
                    context.getString(R.string.label_pause),
                    MediaButtonReceiver.buildMediaButtonPendingIntent(context,
                            PlaybackStateCompat.ACTION_PAUSE))
            : new NotificationCompat.Action(R.drawable.ic_play_arrow_white_24dp,
                    context.getString(R.string.label_play), MediaButtonReceiver
                            .buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_PLAY));

    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(context.getResources(), R.drawable.ic_default_art);
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
    notificationBuilder.setStyle(new NotificationCompat.MediaStyle()
            // show only play/pause in compact view.
            .setShowActionsInCompactView(new int[] { 0 }).setMediaSession(mediaSession.getSessionToken()))
            .addAction(action).setSmallIcon(R.drawable.ic_notification).setShowWhen(false)
            .setContentIntent(controller.getSessionActivity()).setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle()).setLargeIcon(art)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

    return notificationBuilder.build();
}