Example usage for android.support.v4.media.session PlaybackStateCompat ACTION_SKIP_TO_PREVIOUS

List of usage examples for android.support.v4.media.session PlaybackStateCompat ACTION_SKIP_TO_PREVIOUS

Introduction

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

Prototype

long ACTION_SKIP_TO_PREVIOUS

To view the source code for android.support.v4.media.session PlaybackStateCompat ACTION_SKIP_TO_PREVIOUS.

Click Source Link

Document

Indicates this session supports the previous command.

Usage

From source file:com.appdevper.mediaplayer.app.PlaybackManager.java

private long getAvailableActions() {
    long actions = PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID
            | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
            | PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
    if (mPlayback.isPlaying()) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    }/*w  ww. j  ava  2  s.c  o  m*/
    return actions;
}

From source file:com.example.android.uamp.playback.PlaybackManager.java

private long getAvailableActions() {
    long actions = PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID
            | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
            | PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
    if (mPlayback.isPlaying()) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    } else {/*www  .  ja v  a2 s.c  o m*/
        actions |= PlaybackStateCompat.ACTION_PLAY;
    }
    return actions;
}

From source file:org.gateshipone.odyssey.playbackservice.managers.PlaybackServiceStatusHelper.java

/**
 * Updates the Metadata from Androids MediaSession. This sets track/album and stuff
 * for a lockscreen image for example.//from  www .  j a va2 s  .c  om
 *
 * @param track         Current track.
 * @param playbackState State of the PlaybackService.
 */
private void updateMetadata(TrackModel track, PlaybackService.PLAYSTATE playbackState) {
    if (track != null) {
        if (playbackState == PlaybackService.PLAYSTATE.PLAYING) {
            mMediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
                    .setState(PlaybackStateCompat.STATE_PLAYING, 0, 1.0f)
                    .setActions(PlaybackStateCompat.ACTION_SKIP_TO_NEXT + PlaybackStateCompat.ACTION_PAUSE
                            + PlaybackStateCompat.ACTION_PLAY + PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
                            + PlaybackStateCompat.ACTION_STOP + PlaybackStateCompat.ACTION_SEEK_TO)
                    .build());
        } else {
            mMediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
                    .setState(PlaybackStateCompat.STATE_PAUSED, 0, 1.0f)
                    .setActions(PlaybackStateCompat.ACTION_SKIP_TO_NEXT + PlaybackStateCompat.ACTION_PAUSE
                            + PlaybackStateCompat.ACTION_PLAY + PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
                            + PlaybackStateCompat.ACTION_STOP + PlaybackStateCompat.ACTION_SEEK_TO)
                    .build());
        }
        // Try to get old metadata to save image retrieval.
        MediaMetadataCompat oldData = mMediaSession.getController().getMetadata();
        MediaMetadataCompat.Builder metaDataBuilder;
        if (oldData == null) {
            metaDataBuilder = new MediaMetadataCompat.Builder();
        } else {
            metaDataBuilder = new MediaMetadataCompat.Builder(mMediaSession.getController().getMetadata());
        }
        metaDataBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, track.getTrackName());
        metaDataBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, track.getTrackAlbumName());
        metaDataBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, track.getTrackArtistName());
        metaDataBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, track.getTrackArtistName());
        metaDataBuilder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, track.getTrackName());
        metaDataBuilder.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, track.getTrackNumber());
        metaDataBuilder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, track.getTrackDuration());

        mMediaSession.setMetadata(metaDataBuilder.build());
    }
}

From source file:com.devbrackets.android.exomedia.EMLockScreen.java

/**
 * Determines the available playback commands supported for the current media state
 *
 * @param mediaState The current media playback state
 * @return The available playback options
 *//*from  w ww  .  j a  v  a  2 s.  c  om*/
@PlaybackStateCompat.Actions
private long getPlaybackOptions(EMNotification.NotificationMediaState mediaState) {
    long availableActions = PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE
            | PlaybackStateCompat.ACTION_PLAY_PAUSE;

    if (mediaState.isNextEnabled()) {
        availableActions |= PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
    }

    if (mediaState.isPreviousEnabled()) {
        availableActions |= PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
    }

    return availableActions;
}

From source file:com.devbrackets.android.playlistcore.helper.MediaControlsHelper.java

/**
 * Determines the available playback commands supported for the current media state
 *
 * @param mediaState The current media playback state
 * @return The available playback options
 *//*  w  ww . j  a  v a 2 s .  co m*/
@PlaybackStateCompat.Actions
protected long getPlaybackOptions(@NonNull NotificationHelper.NotificationMediaState mediaState) {
    long availableActions = PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE
            | PlaybackStateCompat.ACTION_PLAY_PAUSE;

    if (mediaState.isNextEnabled()) {
        availableActions |= PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
    }

    if (mediaState.isPreviousEnabled()) {
        availableActions |= PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
    }

    return availableActions;
}

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

private void onPlaybackStateChanged(PlaybackStateCompat state) {
    Log.d(TAG, "onPlaybackStateChanged " + state);
    if (state == null) {
        return;// w w w .  ja v a2 s .  c o m
    }
    mQueueAdapter.setActiveQueueItemId(state.getActiveQueueItemId());
    mQueueAdapter.notifyDataSetChanged();
    boolean enablePlay = false;
    StringBuilder statusBuilder = new StringBuilder();
    switch (state.getState()) {
    case PlaybackStateCompat.STATE_PLAYING:
        statusBuilder.append("playing");
        enablePlay = false;
        break;
    case PlaybackStateCompat.STATE_PAUSED:
        statusBuilder.append("paused");
        enablePlay = true;
        break;
    case PlaybackStateCompat.STATE_STOPPED:
        statusBuilder.append("ended");
        enablePlay = true;
        break;
    case PlaybackStateCompat.STATE_ERROR:
        statusBuilder.append("error: ").append(state.getErrorMessage());
        break;
    case PlaybackStateCompat.STATE_BUFFERING:
        statusBuilder.append("buffering");
        break;
    case PlaybackStateCompat.STATE_NONE:
        statusBuilder.append("none");
        enablePlay = false;
        break;
    case PlaybackStateCompat.STATE_CONNECTING:
        statusBuilder.append("connecting");
        break;
    default:
        statusBuilder.append(mPlaybackState);
    }
    statusBuilder.append(" -- At position: ").append(state.getPosition());
    Log.d(TAG, statusBuilder.toString());

    if (enablePlay) {
        mPlayPause.setImageDrawable(
                getActivity().getResources().getDrawable(R.drawable.ic_play_arrow_white_24dp));
    } else {
        mPlayPause.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.ic_pause_white_24dp));
    }

    mSkipPrevious.setEnabled((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0);
    mSkipNext.setEnabled((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0);

    Log.d(TAG, "Queue From MediaController *** Title " + mMediaController.getQueueTitle() + "\n: Queue: "
            + mMediaController.getQueue() + "\n Metadata " + mMediaController.getMetadata());
}

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

private void onPlaybackStateChanged(PlaybackStateCompat state) {
    LogHelper.d(TAG, "onPlaybackStateChanged ", state);
    if (state == null) {
        return;/*  w  ww.j a v a  2  s . co  m*/
    }
    mQueueAdapter.setActiveQueueItemId(state.getActiveQueueItemId());
    mQueueAdapter.notifyDataSetChanged();
    boolean enablePlay = false;
    StringBuilder statusBuilder = new StringBuilder();
    switch (state.getState()) {
    case PlaybackState.STATE_PLAYING:
        statusBuilder.append("playing");
        enablePlay = false;
        break;
    case PlaybackState.STATE_PAUSED:
        statusBuilder.append("paused");
        enablePlay = true;
        break;
    case PlaybackState.STATE_STOPPED:
        statusBuilder.append("ended");
        enablePlay = true;
        break;
    case PlaybackState.STATE_ERROR:
        statusBuilder.append("error: ").append(state.getErrorMessage());
        break;
    case PlaybackState.STATE_BUFFERING:
        statusBuilder.append("buffering");
        break;
    case PlaybackState.STATE_NONE:
        statusBuilder.append("none");
        enablePlay = false;
        break;
    case PlaybackState.STATE_CONNECTING:
        statusBuilder.append("connecting");
        break;
    default:
        statusBuilder.append(mPlaybackState);
    }
    statusBuilder.append(" -- At position: ").append(state.getPosition());
    LogHelper.d(TAG, statusBuilder.toString());

    if (enablePlay) {
        mPlayPause.setImageDrawable(
                ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_white_24dp));
    } else {
        mPlayPause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_white_24dp));
    }

    mSkipPrevious.setEnabled((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0);
    mSkipNext.setEnabled((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0);

    LogHelper.d(TAG, "Queue From MediaController *** Title " + mMediaController.getQueueTitle() + "\n: Queue: "
            + mMediaController.getQueue() + "\n Metadata " + mMediaController.getMetadata());
}

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

private Notification createNotification() {
    Log.d(TAG, "updateNotificationMetadata. mMetadata=" + mMetadata);
    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }/*from w ww.j  a v  a 2 s . co  m*/

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService);

    // 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);
    }

    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.setColor(mNotificationColor).setSmallIcon(R.drawable.ic_notification)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setUsesChronometer(true)
            .setContentIntent(createContentIntent()).setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle()).setLargeIcon(art);

    setNotificationPlaybackState(notificationBuilder);
    if (fetchArtUrl != null) {
        fetchBitmapFromURLAsync(fetchArtUrl, notificationBuilder);
    }

    return notificationBuilder.build();
}

From source file:com.allthatseries.RNAudioPlayer.MediaNotificationManager.java

private Notification createNotification() {
    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }/*  ww  w.jav  a 2s. com*/

    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, "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, "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);
        }
    }

    // Basically we use notification icon but it doesn't exist we use launcher icon
    Context context = mService.getApplicationContext();
    int resId = context.getResources().getIdentifier("ic_notification", "drawable", context.getPackageName());
    if (resId == 0) {
        resId = context.getResources().getIdentifier("ic_launcher", "mipmap", context.getPackageName());
    }

    notificationBuilder
            .setStyle(new NotificationCompat.MediaStyle()
                    .setShowActionsInCompactView(new int[] { playPauseButtonPosition }) // show only play/pause in compact view
                    .setMediaSession(mSessionToken))
            .setColor(0xffdf533b).setSmallIcon(resId).setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setUsesChronometer(true).setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle()).setLargeIcon(art);

    setNotificationPlaybackState(notificationBuilder);

    // We should get MainActivity.class to set pendingIntent
    // If there exists better way, it should place this logic
    String packageName = mService.getApplicationContext().getPackageName();
    Intent launchIntent = mService.getApplicationContext().getPackageManager()
            .getLaunchIntentForPackage(packageName);
    String className = launchIntent.getComponent().getClassName();

    try {
        Class activityClass = Class.forName(className);
        notificationBuilder.setContentIntent(createContentIntent(activityClass));
    } catch (Exception e) {
        // do nothing
    }

    if (fetchArtUrl != null) {
        fetchBitmapFromURLAsync(fetchArtUrl, notificationBuilder);
    }

    return notificationBuilder.build();
}

From source file:com.classiqo.nativeandroid_32bitz.MediaNotificationManager.java

private Notification createNotification() {
    LogHelper.d(TAG, "updateNotificationMetadata.mMetadata = " + mMetadata);

    if (mMetadata == null || mPlaybackState == null) {
        return null;
    }//from   w ww .ja  v a2s  .c om

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mService);
    int playPauseButtonPosition = 0;

    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);

        playPauseButtonPosition = 1;
    }

    addPlayPauseAction(notificationBuilder);

    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) {
        String artUrl = description.getIconUri().toString();
        art = AlbumArtCache.getInstance().getBigImage(artUrl);

        if (art == null) {
            fetchArtUrl = artUrl;
            art = BitmapFactory.decodeResource(mService.getResources(), R.drawable.ic_default_art);
        }
    }

    notificationBuilder
            .setStyle(new NotificationCompat.MediaStyle()
                    .setShowActionsInCompactView(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();
}