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

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

Introduction

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

Prototype

int STATE_PAUSED

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

Click Source Link

Document

State indicating this item is currently paused.

Usage

From source file:com.torrenttunes.android.ui.FullScreenPlayerActivity.java

private void updatePlaybackState(PlaybackStateCompat state) {
    if (state == null) {
        return;/*from ww w.  ja  va 2 s.c o m*/
    }
    mLastPlaybackState = state;
    String castName = mMediaController.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
    String line3Text = "";
    if (castName != null) {
        line3Text = getResources().getString(R.string.casting_to_device, castName);
    }
    mLine3.setText(line3Text);

    switch (state.getState()) {
    case PlaybackStateCompat.STATE_PLAYING:
        mLoading.setVisibility(INVISIBLE);
        mPlayPause.setVisibility(VISIBLE);
        mPlayPause.setImageDrawable(mPauseDrawable);
        mControllers.setVisibility(VISIBLE);
        scheduleSeekbarUpdate();
        break;
    case PlaybackStateCompat.STATE_PAUSED:
        mControllers.setVisibility(VISIBLE);
        mLoading.setVisibility(INVISIBLE);
        mPlayPause.setVisibility(VISIBLE);
        mPlayPause.setImageDrawable(mPlayDrawable);
        stopSeekbarUpdate();
        break;
    case PlaybackStateCompat.STATE_NONE:
    case PlaybackStateCompat.STATE_STOPPED:
        mLoading.setVisibility(INVISIBLE);
        mPlayPause.setVisibility(VISIBLE);
        mPlayPause.setImageDrawable(mPlayDrawable);
        stopSeekbarUpdate();
        break;
    case PlaybackStateCompat.STATE_BUFFERING:
        mPlayPause.setVisibility(INVISIBLE);
        mLoading.setVisibility(VISIBLE);
        mLine3.setText(R.string.loading);
        stopSeekbarUpdate();
        break;
    default:
        LogHelper.d(TAG, "Unhandled state ", state.getState());
    }

    mSkipNext.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) == 0 ? INVISIBLE : VISIBLE);
    mSkipPrev.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) == 0 ? INVISIBLE : VISIBLE);
}

From source file:androidx.media.MediaUtils2.java

static int createPlaybackStateCompatState(int playerState, int bufferingState) {
    switch (playerState) {
    case MediaPlayerBase.PLAYER_STATE_PLAYING:
        switch (bufferingState) {
        case MediaPlayerBase.BUFFERING_STATE_BUFFERING_AND_STARVED:
            return PlaybackStateCompat.STATE_BUFFERING;
        }/*from ww w . j ava  2  s .  c o m*/
        return PlaybackStateCompat.STATE_PLAYING;
    case MediaPlayerBase.PLAYER_STATE_PAUSED:
        return PlaybackStateCompat.STATE_PAUSED;
    case MediaPlayerBase.PLAYER_STATE_IDLE:
        return PlaybackStateCompat.STATE_NONE;
    case MediaPlayerBase.PLAYER_STATE_ERROR:
        return PlaybackStateCompat.STATE_ERROR;
    }
    // For unknown value
    return PlaybackStateCompat.STATE_ERROR;
}

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

/**
 * Helper function to create a playback state based on current adapter's state.
 *
 * @return playback state compat builder
 *///  w  w  w  .  j a  va2 s.c  om
private PlaybackStateCompat createPlaybackStateBasedOnAdapterState() {

    PlaybackStateCompat.Builder playbackStateCompatBuilder = new PlaybackStateCompat.Builder();
    long currentPosition = getCurrentPosition();
    long bufferedPosition = getBufferedPosition();

    // In this glue we only support normal speed
    float playbackSpeed = NORMAL_SPEED;

    // Translate player adapter's state to play back state compat
    // If player adapter is not prepared
    // ==> STATE_STOPPED
    //     (Launcher can only visualize the media session under playing state,
    //     it makes more sense to map this state to PlaybackStateCompat.STATE_STOPPED)
    // If player adapter is prepared
    //     If player is playing
    //     ==> STATE_PLAYING
    //     If player is not playing
    //     ==> STATE_PAUSED
    if (!getPlayerAdapter().isPrepared()) {
        playbackStateCompatBuilder.setState(PlaybackStateCompat.STATE_STOPPED, currentPosition, playbackSpeed)
                .setActions(getPlaybackStateActions());
    } else if (getPlayerAdapter().isPlaying()) {
        playbackStateCompatBuilder.setState(PlaybackStateCompat.STATE_PLAYING, currentPosition, playbackSpeed)
                .setActions(getPlaybackStateActions());
    } else {
        playbackStateCompatBuilder.setState(PlaybackStateCompat.STATE_PAUSED, currentPosition, playbackSpeed)
                .setActions(getPlaybackStateActions());
    }

    // always fill buffered position
    return playbackStateCompatBuilder.setBufferedPosition(bufferedPosition).build();
}

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

/**
 * Called when media player is done playing current song.
 *
 * @see OnCompletionListener//from  w  w w.  ja v  a2s  . c om
 */
@Override
public void onCompletion(MediaPlayer player) {
    if (mCallback != null) {
        mState = PlaybackStateCompat.STATE_PAUSED;
        mCallback.onCompletion();
    }
}

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

/**
 * Prepare the first item in the list. And setup the listener for media player.
 *///from   ww w  .j  av  a 2  s  .co  m
private void initializePlayer() {
    // This service can be created for multiple times, the objects will only be created when
    // it is null
    if (mPlayer != null) {
        return;
    }
    mPlayer = new MediaPlayer();

    // Set playback state to none to create a valid playback state. So controls row can get
    // information about the supported actions.
    mMediaSession.setPlaybackState(createPlaybackStateBuilder(PlaybackStateCompat.STATE_NONE).build());
    // Activate media session
    if (!mMediaSession.isActive()) {
        mMediaSession.setActive(true);
    }

    // Set up listener and audio stream type for underlying music player.
    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    // set up listener when the player is prepared.
    mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mInitialized = true;
            // Every time when the player is prepared (when new data source is set),
            // all listeners will be notified to toggle the UI to "pause" status.
            notifyUiWhenPlayerIsPrepared();

            // When media player is prepared, the callback functions will be executed to update
            // the meta data and playback state.
            onMediaSessionMetaDataChanged();
            mMediaSession
                    .setPlaybackState(createPlaybackStateBuilder(PlaybackStateCompat.STATE_PAUSED).build());
        }
    });

    // set up listener for player's error.
    mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
            if (DEBUG) {
                PlaybackStateCompat.Builder builder = createPlaybackStateBuilder(
                        PlaybackStateCompat.STATE_ERROR);
                builder.setErrorMessage(PlaybackStateCompat.ERROR_CODE_APP_ERROR, MEDIA_PLAYER_ERROR_MESSAGE);
                mMediaSession.setPlaybackState(builder.build());
            }
            return true;
        }
    });

    // set up listener to respond the event when current music item is finished
    mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        /**
         * Expected Interaction Behavior:
         * 1. If current media item's playing speed not equal to normal speed.
         *
         *    A. MEDIA_ACTION_REPEAT_ALL
         *       a. If current media item is the last one. The first music item in the list will
         *          be prepared, but it won't play until user press play button.
         *
         *          When user press the play button, the speed will be reset to normal (1.0f)
         *          no matter what the previous media item's playing speed is.
         *
         *       b. If current media item isn't the last one, next media item will be prepared,
         *          but it won't play.
         *
         *          When user press the play button, the speed will be reset to normal (1.0f)
         *          no matter what the previous media item's playing speed is.
         *
         *    B. MEDIA_ACTION_REPEAT_ONE
         *       Different with previous scenario, current item will go back to the start point
         *       again and play automatically. (The reason to enable auto play here is for
         *       testing purpose and to make sure our designed API is flexible enough to support
         *       different situations.)
         *
         *       No matter what the previous media item's playing speed is, in this situation
         *       current media item will be replayed in normal speed.
         *
         *    C. MEDIA_ACTION_REPEAT_NONE
         *       a. If current media is the last one. The service will be closed, no music item
         *          will be prepared to play. From the UI perspective, the progress bar will not
         *          be reset to the starting point.
         *
         *       b. If current media item isn't the last one, next media item will be prepared,
         *          but it won't play.
         *
         *          When user press the play button, the speed will be reset to normal (1.0f)
         *          no matter what the previous media item's playing speed is.
         *
         * @param mp Object of MediaPlayer
         */
        @Override
        public void onCompletion(MediaPlayer mp) {

            // When current media item finishes playing, always reset rewind/ fastforward state
            mFastForwardSpeedFactorIndex = 0;
            mRewindSpeedFactorIndex = 0;
            // Set player's playback speed back to normal
            mPlayer.setPlaybackParams(mPlayer.getPlaybackParams()
                    .setSpeed(mFastForwardSpeedFactors[mFastForwardSpeedFactorIndex]));
            // Pause the player, and update the status accordingly.
            mPlayer.pause();
            mMediaSession
                    .setPlaybackState(createPlaybackStateBuilder(PlaybackStateCompat.STATE_PAUSED).build());

            if (mRepeatState == MEDIA_ACTION_REPEAT_ALL && mCurrentIndex == mMediaItemList.size() - 1) {
                // if the repeat mode is enabled but the shuffle mode is not enabled,
                // will go back to the first music item to play
                if (mShuffleMode == PlaybackStateCompat.SHUFFLE_MODE_NONE) {
                    mCurrentIndex = 0;
                } else {
                    // Or will choose a music item from playing list randomly.
                    mCurrentIndex = generateMediaItemIndex();
                }
                mCurrentMediaItem = mMediaItemList.get(mCurrentIndex);
                // The ui will also be changed from playing state to pause state through
                // setDataSource() operation
                setDataSource();
            } else if (mRepeatState == MEDIA_ACTION_REPEAT_ONE) {
                // Play current music item again.
                // The ui will stay to be "playing" status for the reason that there is no
                // setDataSource() function call.
                mPlayer.start();
                mMediaSession.setPlaybackState(
                        createPlaybackStateBuilder(PlaybackStateCompat.STATE_PLAYING).build());
            } else if (mCurrentIndex < mMediaItemList.size() - 1) {
                if (mShuffleMode == PlaybackStateCompat.SHUFFLE_MODE_NONE) {
                    mCurrentIndex++;
                } else {
                    mCurrentIndex = generateMediaItemIndex();
                }
                mCurrentMediaItem = mMediaItemList.get(mCurrentIndex);
                // The ui will also be changed from playing state to pause state through
                // setDataSource() operation
                setDataSource();
            } else {
                // close the service when the playlist is finished
                // The PlaybackState will be updated to STATE_STOPPED. And onPlayComplete
                // callback will be called by attached glue.
                mMediaSession.setPlaybackState(
                        createPlaybackStateBuilder(PlaybackStateCompat.STATE_STOPPED).build());
                stopSelf();
            }
        }
    });

    final MediaPlayer.OnBufferingUpdateListener mOnBufferingUpdateListener = new MediaPlayer.OnBufferingUpdateListener() {
        @Override
        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            mBufferedProgress = getDuration() * percent / 100;
            PlaybackStateCompat.Builder builder = createPlaybackStateBuilder(
                    PlaybackStateCompat.STATE_BUFFERING);
            builder.setBufferedPosition(mBufferedProgress);
            mMediaSession.setPlaybackState(builder.build());
        }
    };
    mPlayer.setOnBufferingUpdateListener(mOnBufferingUpdateListener);
}

From source file:cat.terrones.devops.radiofx.ui.FullScreenPlayerActivity.java

private void updatePlaybackState(PlaybackStateCompat state) {
    if (state == null) {
        return;//from  w  ww  .  ja  va 2s. c  o m
    }
    mLastPlaybackState = state;
    if (getSupportMediaController() != null && getSupportMediaController().getExtras() != null) {
        String castName = getSupportMediaController().getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
        String line3Text = castName == null ? ""
                : getResources().getString(R.string.casting_to_device, castName);
        mLine3.setText(line3Text);
    }

    switch (state.getState()) {
    case PlaybackStateCompat.STATE_PLAYING:
        mLoading.setVisibility(INVISIBLE);
        mPlayPause.setVisibility(VISIBLE);
        mPlayPause.setImageDrawable(mPauseDrawable);
        mControllers.setVisibility(VISIBLE);
        scheduleSeekbarUpdate();
        break;
    case PlaybackStateCompat.STATE_PAUSED:
        mControllers.setVisibility(VISIBLE);
        mLoading.setVisibility(INVISIBLE);
        mPlayPause.setVisibility(VISIBLE);
        mPlayPause.setImageDrawable(mPlayDrawable);
        stopSeekbarUpdate();
        break;
    case PlaybackStateCompat.STATE_NONE:
    case PlaybackStateCompat.STATE_STOPPED:
        mLoading.setVisibility(INVISIBLE);
        mPlayPause.setVisibility(VISIBLE);
        mPlayPause.setImageDrawable(mPlayDrawable);
        stopSeekbarUpdate();
        break;
    case PlaybackStateCompat.STATE_BUFFERING:
        mPlayPause.setVisibility(INVISIBLE);
        mLoading.setVisibility(VISIBLE);
        mLine3.setText(R.string.loading);
        stopSeekbarUpdate();
        break;
    default:
        LogHelper.d(TAG, "Unhandled state ", state.getState());
    }

    mSkipNext.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) == 0 ? INVISIBLE : VISIBLE);
    mSkipPrev.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) == 0 ? INVISIBLE : VISIBLE);
}

From source file:rocks.stalin.android.app.ui.FullScreenPlayerActivity.java

private void updatePlaybackState(PlaybackStateCompat state) {
    if (state == null) {
        return;//from   w w  w . j  av  a  2  s  . c om
    }
    mLastPlaybackState = state;
    if (MediaControllerCompat.getMediaController(this) != null
            && MediaControllerCompat.getMediaController(this).getExtras() != null) {
        String castName = MediaControllerCompat.getMediaController(this).getExtras()
                .getString(MusicService.EXTRA_CONNECTED_CAST);
        String line3Text = castName == null ? ""
                : getResources().getString(R.string.casting_to_device, castName);
        mLine3.setText(line3Text);
    }

    switch (state.getState()) {
    case PlaybackStateCompat.STATE_PLAYING:
        mLoading.setVisibility(INVISIBLE);
        mPlayPause.setVisibility(VISIBLE);
        mPlayPause.setImageDrawable(mPauseDrawable);
        mControllers.setVisibility(VISIBLE);
        scheduleSeekbarUpdate();
        break;
    case PlaybackStateCompat.STATE_PAUSED:
        mControllers.setVisibility(VISIBLE);
        mLoading.setVisibility(INVISIBLE);
        mPlayPause.setVisibility(VISIBLE);
        mPlayPause.setImageDrawable(mPlayDrawable);
        stopSeekbarUpdate();
        break;
    case PlaybackStateCompat.STATE_NONE:
    case PlaybackStateCompat.STATE_STOPPED:
        mLoading.setVisibility(INVISIBLE);
        mPlayPause.setVisibility(VISIBLE);
        mPlayPause.setImageDrawable(mPlayDrawable);
        stopSeekbarUpdate();
        break;
    case PlaybackStateCompat.STATE_BUFFERING:
        mPlayPause.setVisibility(INVISIBLE);
        mLoading.setVisibility(VISIBLE);
        mLine3.setText(R.string.loading);
        stopSeekbarUpdate();
        break;
    default:
        LogHelper.d(TAG, "Unhandled state ", state.getState());
    }

    mSkipNext.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) == 0 ? INVISIBLE : VISIBLE);
    mSkipPrev.setVisibility(
            (state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) == 0 ? INVISIBLE : VISIBLE);
}

From source file:androidx.media.MediaUtils2.java

static int toPlayerState(int playbackStateCompatState) {
    switch (playbackStateCompatState) {
    case PlaybackStateCompat.STATE_ERROR:
        return MediaPlayerBase.PLAYER_STATE_ERROR;
    case PlaybackStateCompat.STATE_NONE:
        return MediaPlayerBase.PLAYER_STATE_IDLE;
    case PlaybackStateCompat.STATE_PAUSED:
    case PlaybackStateCompat.STATE_STOPPED:
    case PlaybackStateCompat.STATE_BUFFERING: // means paused for buffering.
        return MediaPlayerBase.PLAYER_STATE_PAUSED;
    case PlaybackStateCompat.STATE_FAST_FORWARDING:
    case PlaybackStateCompat.STATE_PLAYING:
    case PlaybackStateCompat.STATE_REWINDING:
    case PlaybackStateCompat.STATE_SKIPPING_TO_NEXT:
    case PlaybackStateCompat.STATE_SKIPPING_TO_PREVIOUS:
    case PlaybackStateCompat.STATE_SKIPPING_TO_QUEUE_ITEM:
    case PlaybackStateCompat.STATE_CONNECTING: // Note: there's no perfect match for this.
        return MediaPlayerBase.PLAYER_STATE_PLAYING;
    }/*  w  w w . j av a  2s .c o  m*/
    return MediaPlayerBase.PLAYER_STATE_ERROR;
}

From source file:org.runbuddy.tomahawk.mediaplayers.PluginMediaPlayer.java

/**
 * Pause playing the current {@link Query}
 *///from w  w  w.  j ava 2s. c o  m
@Override
public void pause() {
    Log.d(TAG, "pause()");
    mPlayState = PlaybackStateCompat.STATE_PAUSED;
    handlePlayState();
}

From source file:org.gateshipone.malp.application.background.NotificationManager.java

/**
 * Updates the Metadata from Androids MediaSession. This sets track/album and stuff
 * for a lockscreen image for example.//from   ww  w.j a va2s .co  m
 *
 * @param track         Current track.
 * @param playbackState State of the PlaybackService.
 */
private void updateMetadata(MPDTrack track, MPDCurrentStatus.MPD_PLAYBACK_STATE playbackState) {
    if (track != null) {
        if (playbackState == MPDCurrentStatus.MPD_PLAYBACK_STATE.MPD_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.getTrackTitle());
        metaDataBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, track.getTrackAlbum());
        metaDataBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, track.getTrackArtist());
        metaDataBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, track.getTrackArtist());
        metaDataBuilder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, track.getTrackTitle());
        metaDataBuilder.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, track.getTrackNumber());
        metaDataBuilder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, track.getLength());

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