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.scooter1556.sms.android.activity.FullScreenPlayerActivity.java

private void updatePlaybackState(PlaybackStateCompat state) {
    if (state == null) {
        return;//  w w w .  ja v  a 2s . c  om
    }

    lastPlaybackState = state;
    if (mediaController != null && mediaController.getExtras() != null) {
        String castName = mediaController.getExtras().getString(MediaService.EXTRA_CONNECTED_CAST);
        String extraInfo = castName == null ? "" : getResources().getString(R.string.cast_to_device, castName);
        extra.setText(extraInfo);
    }

    switch (state.getState()) {

    case PlaybackStateCompat.STATE_PLAYING:
        loading.setVisibility(INVISIBLE);
        playPause.setVisibility(VISIBLE);
        playPause.setImageDrawable(pauseDrawable);
        controllers.setVisibility(VISIBLE);
        scheduleSeekbarUpdate();
        break;

    case PlaybackStateCompat.STATE_PAUSED:
        controllers.setVisibility(VISIBLE);
        loading.setVisibility(INVISIBLE);
        playPause.setVisibility(VISIBLE);
        playPause.setImageDrawable(playDrawable);
        stopSeekbarUpdate();
        break;

    case PlaybackStateCompat.STATE_NONE:

    case PlaybackStateCompat.STATE_STOPPED:
        loading.setVisibility(INVISIBLE);
        playPause.setVisibility(VISIBLE);
        playPause.setImageDrawable(playDrawable);
        stopSeekbarUpdate();
        break;

    case PlaybackStateCompat.STATE_BUFFERING:
        playPause.setVisibility(INVISIBLE);
        loading.setVisibility(VISIBLE);
        extra.setText(R.string.state_loading);
        stopSeekbarUpdate();
        break;

    default:
        Log.d(TAG, "Unhandled state: " + state.getState());
    }

    skipNext.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) == 0 ? INVISIBLE : VISIBLE);
    skipPrev.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) == 0 ? INVISIBLE : VISIBLE);
    shuffle.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE) == 0 ? INVISIBLE : VISIBLE);
    repeat.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SET_REPEAT_MODE) == 0 ? INVISIBLE : VISIBLE);

    // Custom Actions
    for (PlaybackStateCompat.CustomAction action : state.getCustomActions()) {
        switch (action.getAction()) {
        case MediaService.STATE_SHUFFLE_ON:
            shuffle.setColorFilter(ContextCompat.getColor(getBaseContext(), R.color.accent));
            break;

        case MediaService.STATE_SHUFFLE_OFF:
            shuffle.clearColorFilter();
            break;

        case MediaService.STATE_REPEAT_NONE:
            repeat.setImageResource(R.drawable.ic_repeat_white_24dp);
            repeat.clearColorFilter();
            break;

        case MediaService.STATE_REPEAT_ALL:
            repeat.setImageResource(R.drawable.ic_repeat_white_24dp);
            repeat.setColorFilter(ContextCompat.getColor(getBaseContext(), R.color.accent));
            break;

        case MediaService.STATE_REPEAT_ONE:
            repeat.setImageResource(R.drawable.ic_repeat_one_white_24dp);
            repeat.setColorFilter(ContextCompat.getColor(getBaseContext(), R.color.accent));
            break;
        }
    }
}

From source file:com.andryr.musicplayer.PlaybackService.java

private void updateMediaSession(String what) {

    if (!mMediaSession.isActive()) {
        mMediaSession.setActive(true);/*  w ww  . j  a v  a2 s . c o m*/
    }

    if (what.equals(PLAYSTATE_CHANGED)) {

        int playState = isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED;
        mMediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
                .setState(playState, getPlayerPosition(), 1.0F)
                .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE
                        | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT
                        | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                .build());
    }

    if (what.equals(META_CHANGED)) {
        int largeArtSize = (int) getResources().getDimension(R.dimen.art_size);
        Bitmap artwork = ArtworkCache.getInstance().getCachedBitmap(getAlbumId(), largeArtSize, largeArtSize);

        MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder()
                .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getArtistName())
                .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getAlbumName())
                .putString(MediaMetadataCompat.METADATA_KEY_TITLE, getSongTitle())
                .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, getTrackDuration())
                .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, artwork);
        mMediaSession.setMetadata(builder.build());

    }
}

From source file:com.wojtechnology.sunami.TheBrain.java

private void setMetadata(FireMixtape song) {
    new GetArtworkTask().execute(song);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mNotification = getLollipopNotifBuilder(true).setLargeIcon(mThumbnail).setContentTitle(song.title)
                .setContentText(song.artist).build();
        startForeground(534, mNotification);
    } else {//w  ww.j a  v  a2  s.  co  m
        PlaybackStateCompat state = new PlaybackStateCompat.Builder()
                .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE
                        | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SEEK_TO
                        | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                .setState(PlaybackStateCompat.STATE_PLAYING, 0, 0.0f).build();
        MediaSessionCompatHelper.applyState(mSession, state);
        mSession.setMetadata(
                new MediaMetadataCompat.Builder().putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.title)
                        .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.artist)
                        .putLong(MediaMetadata.METADATA_KEY_DURATION, Long.parseLong(song.duration)).build());
    }
}

From source file:com.scooter1556.sms.lib.android.service.AudioPlayerService.java

private long getAvailableActions() {
    long actions = PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID;

    if (mediaElementList == null || mediaElementList.isEmpty()) {
        return actions;
    }/*  w  ww .  ja  v  a2  s .co m*/

    if (mediaState == PlaybackStateCompat.STATE_PLAYING) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    } else {
        actions |= PlaybackStateCompat.ACTION_PLAY;
    }

    if (currentListPosition > 0) {
        actions |= PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
    }

    if (currentListPosition < mediaElementList.size() - 1) {
        actions |= PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
    }

    return actions;
}

From source file:com.wojtechnology.sunami.TheBrain.java

public void setProgressUI(int pos, boolean isPlaying) {
    int speed = isPlaying ? 1 : 0;
    int playState = isPlaying ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED;
    PlaybackStateCompat state = new PlaybackStateCompat.Builder()
            .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE
                    | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SEEK_TO
                    | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
            .setState(playState, pos, speed).build();
    MediaSessionCompatHelper.applyState(mSession, state);
}

From source file:com.example.android.leanback.MediaSessionService.java

/**
 * Return supported actions related to current playback state.
 * Currently the return value from this function is a constant.
 * For demonstration purpose, the customized fast forward action and customized rewind action
 * are supported in our case./*from   w  ww  .  j ava  2 s.c  o m*/
 *
 * @return playback state actions.
 */
private long getPlaybackStateActions() {
    long res = PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE
            | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT
            | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS | PlaybackStateCompat.ACTION_FAST_FORWARD
            | PlaybackStateCompat.ACTION_REWIND | PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE
            | PlaybackStateCompat.ACTION_SET_REPEAT_MODE;
    return res;
}

From source file:androidx.media.MediaSession2ImplBase.java

@Override
PlaybackStateCompat getPlaybackStateCompat() {
    synchronized (mLock) {
        int state = MediaUtils2.createPlaybackStateCompatState(getPlayerState(), getBufferingState());
        // TODO: Consider following missing stuff
        //       - setCustomAction(): Fill custom layout
        //       - setErrorMessage(): Fill error message when notifyError() is called.
        //       - setActiveQueueItemId(): Fill here with the current media item...
        //       - setExtra(): No idea at this moment.
        // TODO: generate actions from the allowed commands.
        long allActions = PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_PAUSE
                | PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_REWIND
                | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS | PlaybackStateCompat.ACTION_SKIP_TO_NEXT
                | PlaybackStateCompat.ACTION_FAST_FORWARD | PlaybackStateCompat.ACTION_SET_RATING
                | PlaybackStateCompat.ACTION_SEEK_TO | PlaybackStateCompat.ACTION_PLAY_PAUSE
                | PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH
                | PlaybackStateCompat.ACTION_SKIP_TO_QUEUE_ITEM | PlaybackStateCompat.ACTION_PLAY_FROM_URI
                | PlaybackStateCompat.ACTION_PREPARE | PlaybackStateCompat.ACTION_PREPARE_FROM_MEDIA_ID
                | PlaybackStateCompat.ACTION_PREPARE_FROM_SEARCH | PlaybackStateCompat.ACTION_PREPARE_FROM_URI
                | PlaybackStateCompat.ACTION_SET_REPEAT_MODE | PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE
                | PlaybackStateCompat.ACTION_SET_CAPTIONING_ENABLED;
        return new PlaybackStateCompat.Builder().setState(state, getCurrentPosition(), getPlaybackSpeed())
                .setActions(allActions).setBufferedPosition(getBufferedPosition()).build();
    }//from   w  w  w.ja  va 2 s .co  m
}

From source file:org.runbuddy.tomahawk.ui.fragments.PlaybackFragment.java

public void showSwipeHints(PlaybackStateCompat playbackState) {
    if (!mSwipeHintsShown && mSwipeHintLeft != null && mSwipeHintRight != null && mSwipeHintBottom != null) {
        mSwipeHintsShown = true;//from   www.ja  v a  2  s  .  c o m
        AnimationUtils.fade(mSwipeHintBottom, AnimationUtils.DURATION_PLAYBACKTOPPANEL, true);
        long actions = playbackState.getActions();
        final boolean hasPreviousEntry = (actions & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0;
        final boolean hasNextEntry = (actions & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0;
        if (hasPreviousEntry) {
            AnimationUtils.fade(mSwipeHintLeft, AnimationUtils.DURATION_PLAYBACKTOPPANEL, true);
        }
        if (hasNextEntry) {
            AnimationUtils.fade(mSwipeHintRight, AnimationUtils.DURATION_PLAYBACKTOPPANEL, true);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeHintsShown = false;
                if (hasPreviousEntry) {
                    AnimationUtils.fade(mSwipeHintLeft, AnimationUtils.DURATION_PLAYBACKTOPPANEL, false);
                }
                if (hasNextEntry) {
                    AnimationUtils.fade(mSwipeHintRight, AnimationUtils.DURATION_PLAYBACKTOPPANEL, false);
                }
                AnimationUtils.fade(mSwipeHintBottom, AnimationUtils.DURATION_PLAYBACKTOPPANEL, false);
            }
        }, 1500);
    }
}

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

private long getAvailableActions() {
    long actions = PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID
            | PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH;
    if (mPlayingQueue == null || mPlayingQueue.isEmpty()) {
        return actions;
    }//from w w  w  .ja  v  a2 s  .co  m
    if (mPlayback.isPlaying()) {
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    }
    if (mCurrentIndexOnQueue > 0) {
        actions |= PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
    }
    if (mCurrentIndexOnQueue < mPlayingQueue.size() - 1) {
        actions |= PlaybackStateCompat.ACTION_SKIP_TO_NEXT;
    }
    return actions;
}

From source file:com.techmighty.baseplayer.MusicService.java

private void updateMediaSession(final String what) {
    int playState = mIsSupposedToBePlaying ? PlaybackStateCompat.STATE_PLAYING
            : PlaybackStateCompat.STATE_PAUSED;

    if (what.equals(PLAYSTATE_CHANGED) || what.equals(POSITION_CHANGED)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSession.setPlaybackState(new PlaybackStateCompat.Builder().setState(playState, position(), 1.0f)
                    .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE
                            | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT
                            | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                    .build());/*from w ww.  j  a  v  a2  s.  c  o  m*/
        }
    } else if (what.equals(META_CHANGED) || what.equals(QUEUE_CHANGED)) {
        Bitmap albumArt = ImageLoader.getInstance()
                .loadImageSync(BasePlayerUtils.getAlbumArtUri(getAlbumId()).toString());
        if (albumArt != null) {

            Bitmap.Config config = albumArt.getConfig();
            if (config == null) {
                config = Bitmap.Config.ARGB_8888;
            }
            albumArt = albumArt.copy(config, false);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSession.setMetadata(new MediaMetadataCompat.Builder()
                    .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getArtistName())
                    .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, getAlbumArtistName())
                    .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getAlbumName())
                    .putString(MediaMetadataCompat.METADATA_KEY_TITLE, getTrackName())
                    .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration())
                    .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getQueuePosition() + 1)
                    .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getQueue().length)
                    .putString(MediaMetadataCompat.METADATA_KEY_GENRE, getGenreName())
                    .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,
                            mShowAlbumArtOnLockscreen ? albumArt : null)
                    .build());

            mSession.setPlaybackState(new PlaybackStateCompat.Builder().setState(playState, position(), 1.0f)
                    .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE
                            | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT
                            | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                    .build());
        }
    }
}