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:org.chromium.chrome.browser.media.ui.MediaNotificationManager.java

private void updateMediaSession() {
    if (!mMediaNotificationInfo.supportsPlayPause())
        return;/*from w w  w  . j a  v a  2 s.  c  o m*/

    if (mMediaSession == null)
        mMediaSession = createMediaSession();

    try {
        // Tell the MediaRouter about the session, so that Chrome can control the volume
        // on the remote cast device (if any).
        // Pre-MR1 versions of JB do not have the complete MediaRouter APIs,
        // so getting the MediaRouter instance will throw an exception.
        MediaRouter.getInstance(mContext).setMediaSessionCompat(mMediaSession);
    } catch (NoSuchMethodError e) {
        // Do nothing. Chrome can't be casting without a MediaRouter, so there is nothing
        // to do here.
    }

    mMediaSession.setMetadata(createMetadata());

    PlaybackStateCompat.Builder playbackStateBuilder = new PlaybackStateCompat.Builder()
            .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE);
    if (mMediaNotificationInfo.isPaused) {
        playbackStateBuilder.setState(PlaybackStateCompat.STATE_PAUSED,
                PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f);
    } else {
        // If notification only supports stop, still pretend
        playbackStateBuilder.setState(PlaybackStateCompat.STATE_PLAYING,
                PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f);
    }
    mMediaSession.setPlaybackState(playbackStateBuilder.build());
}

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

public void start() {
    if (mediaState == PlaybackStateCompat.STATE_STOPPED) {
        play();//from w  w  w  .j a  v  a  2s.  com
    } else if (mediaState == PlaybackStateCompat.STATE_PAUSED) {
        player.start();

        // Update state
        mediaState = PlaybackStateCompat.STATE_PLAYING;
        updatePlaybackState();

        wakeLock.acquire();
        wifiLock.acquire();
    }

    isPaused = false;

    // Update listeners
    for (AudioPlayerListener listener : listeners) {
        listener.PlaybackStateChanged();
    }
}

From source file:nuclei.media.playback.ExoPlayerPlayback.java

@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
    if (LOG.isLoggable(Log.DEBUG))
        LOG.d("onStateChanged=" + playbackState + ", " + playWhenReady);
    if (!mPrepared && playbackState == ExoPlayer.STATE_READY && mMediaPlayer != null) {
        mPrepared = true;//from w  ww  .ja v a 2  s . c  om
        if (!mWakeLock.isHeld())
            mWakeLock.acquire();
        if (!mWifiLock.isHeld())
            mWifiLock.acquire();
        configMediaPlayerState(true, false);
        setSurface(mSurfaceId, mSurface);
        mMediaPlayer.seekTo(mCurrentPosition);
        mMediaPlayer.setPlayWhenReady(mPlayWhenReady);
    } else if (mMediaPlayer != null && mState != PlaybackStateCompat.STATE_ERROR
            && mState != PlaybackStateCompat.STATE_BUFFERING)
        mCurrentPosition = mMediaPlayer.getCurrentPosition();

    if (mMediaPlayer != null && mMediaMetadata != null) {
        final long duration = getDuration();
        if (mMediaMetadata.getLong(MediaMetadataCompat.METADATA_KEY_DURATION) != duration)
            mMediaMetadata.setDuration(duration);
    }

    switch (playbackState) {
    case ExoPlayer.STATE_BUFFERING:
        mState = PlaybackStateCompat.STATE_BUFFERING;
        mIllegalStateRetries = 0;
        break;
    case ExoPlayer.STATE_ENDED:
        mState = PlaybackStateCompat.STATE_NONE;
        mIllegalStateRetries = 0;
        break;
    case ExoPlayer.STATE_IDLE:
        if (mState != PlaybackStateCompat.STATE_ERROR)
            mState = PlaybackStateCompat.STATE_NONE;
        break;
    case ExoPlayer.STATE_READY:
        mIllegalStateRetries = 0;
        if (isMediaPlayerPlaying()) {
            mState = PlaybackStateCompat.STATE_PLAYING;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (mPlaybackParams != null)
                    mMediaPlayer.setPlaybackParameters(mPlaybackParams);
            }
        } else
            mState = PlaybackStateCompat.STATE_PAUSED;
        break;
    default:
        mState = PlaybackStateCompat.STATE_NONE;
        break;
    }

    if (mCallback != null)
        mCallback.onPlaybackStatusChanged(mState);

    if (playbackState == ExoPlayer.STATE_ENDED) {
        mRestart = true;
        if (mCallback != null)
            mCallback.onCompletion();
    }
}

From source file:com.scooter1556.sms.android.fragment.tv.TvAudioPlayerFragment.java

private void updateProgress() {
    if (lastPlaybackState == null || playbackControlsRow == null) {
        return;/*w w  w  .j a  va  2 s.  c  o  m*/
    }

    long currentPosition = lastPlaybackState.getPosition();

    if (lastPlaybackState.getState() != PlaybackStateCompat.STATE_PAUSED) {
        // Calculate the elapsed time between the last position update and now and unless
        // paused, we can assume (delta * speed) + current position is approximately the
        // latest position. This ensures that we do not repeatedly call the getPlaybackState()
        // on MediaControllerCompat.
        long timeDelta = SystemClock.elapsedRealtime() - lastPlaybackState.getLastPositionUpdateTime();
        currentPosition += (int) timeDelta * lastPlaybackState.getPlaybackSpeed();
    }

    playbackControlsRow.setCurrentTime((int) (currentPosition));
}

From source file:com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector.java

private int mapPlaybackState(int exoPlayerPlaybackState, boolean playWhenReady) {
    switch (exoPlayerPlaybackState) {
    case Player.STATE_BUFFERING:
        return PlaybackStateCompat.STATE_BUFFERING;
    case Player.STATE_READY:
        return playWhenReady ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED;
    case Player.STATE_ENDED:
        return PlaybackStateCompat.STATE_PAUSED;
    default:// w w  w . j  a v a  2 s.c  om
        return PlaybackStateCompat.STATE_NONE;
    }
}

From source file:com.scooter1556.sms.android.fragment.tv.TvVideoPlayerFragment.java

@Override
public void onLoadingChanged(boolean isLoading) {
    Log.d(TAG, "onLoadingChanged(" + isLoading + ")");

    if (!isLoading && playbackState != PlaybackStateCompat.STATE_PAUSED) {
        initialised = true;//from  ww  w .j  a v a  2  s.  com
        configMediaPlayerState();
    }
}

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

/**
 * Update the current media player state, optionally showing an error message.
 *
 * @param error if not null, error message to present to the user.
 *//*w ww  .  j  av a 2  s  .c  o m*/
private void updatePlaybackState(String error) {
    LogHelper.d(TAG, "updatePlaybackState, playback state=" + mPlayback.getState());
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null && mPlayback.isConnected()) {
        position = mPlayback.getCurrentStreamPosition();
    }

    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(getAvailableActions());

    setCustomAction(stateBuilder);
    int state = mPlayback.getState();

    // If there is an error message, send it to the playback state:
    if (error != null) {
        // Error states are really only supposed to be used for errors that cause playback to
        // stop unexpectedly and persist until the user takes action to fix it.
        stateBuilder.setErrorMessage(error);
        state = PlaybackStateCompat.STATE_ERROR;
    }
    stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    // Set the activeQueueItemId if the current index is valid.
    if (QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
        MediaSessionCompat.QueueItem item = mPlayingQueue.get(mCurrentIndexOnQueue);
        stateBuilder.setActiveQueueItemId(item.getQueueId());
    }

    mSession.setPlaybackState(stateBuilder.build());

    if (state == PlaybackStateCompat.STATE_PLAYING || state == PlaybackStateCompat.STATE_PAUSED) {
        mMediaNotificationManager.startNotification();
    }
}

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

private void pause() {
    if (mPlayer != null && mPlayer.isPlaying()) {
        // abandon audio focus immediately when the music item is paused.
        mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);

        mPlayer.pause();//from   ww w .j  a  v a 2  s  .c  om
        // Update playbackState.
        mMediaSession.setPlaybackState(createPlaybackStateBuilder(PlaybackStateCompat.STATE_PAUSED).build());
    }
}

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

/**
 * Update the current media player state, optionally showing an error message.
 *
 * @param error if not null, error message to present to the user.
 *//* www.j  a  v  a  2s.c om*/
private void updatePlaybackState(String error) {
    Log.d(TAG, "updatePlaybackState, playback state=" + mPlayback.getState());
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null && mPlayback.isConnected()) {
        position = mPlayback.getCurrentStreamPosition();
    }

    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(getAvailableActions());

    setCustomAction(stateBuilder);
    int state = mPlayback.getState();

    // If there is an error message, send it to the playback state:
    if (error != null) {
        // Error states are really only supposed to be used for errors that cause playback to
        // stop unexpectedly and persist until the user takes action to fix it.
        stateBuilder.setErrorMessage(error);
        state = PlaybackStateCompat.STATE_ERROR;
    }
    stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    // Set the activeQueueItemId if the current index is valid.
    if (QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
        MediaSessionCompat.QueueItem item = mPlayingQueue.get(mCurrentIndexOnQueue);
        stateBuilder.setActiveQueueItemId(item.getQueueId());
    }

    mSession.setPlaybackState(stateBuilder.build());

    if (state == PlaybackStateCompat.STATE_PLAYING || state == PlaybackStateCompat.STATE_PAUSED) {
        mMediaNotificationManager.startNotification();
    }
}

From source file:com.scooter1556.sms.android.fragment.tv.TvVideoPlayerFragment.java

@Override
public void onPlayerStateChanged(boolean playWhenReady, int state) {
    switch (state) {
    case ExoPlayer.STATE_BUFFERING:
        Log.d(TAG, "onPlayerStateChanged(BUFFERING)");

        // Update state
        playbackState = PlaybackStateCompat.STATE_BUFFERING;

        break;/*from  ww w  .  ja  va 2  s.c  om*/

    case ExoPlayer.STATE_ENDED:
        Log.d(TAG, "onPlayerStateChanged(ENDED)");

        // End job if required
        if (currentJobId != null) {
            Log.d(TAG, "Ending job with id " + currentJobId);
            RESTService.getInstance().endJob(currentJobId);
            currentJobId = null;
        }

        // The media player finished playing the current item, so we go ahead and start the next.
        if (callback != null) {
            callback.onCompletion();
        }

        break;

    case ExoPlayer.STATE_IDLE:
        Log.d(TAG, "onPlayerStateChanged(IDLE)");

        break;

    case ExoPlayer.STATE_READY:
        Log.d(TAG, "onPlayerStateChanged(READY)");

        if (playWhenReady) {
            // Set timeline
            playbackControlsRow.setCurrentTime((int) mediaPlayer.getCurrentPosition());

            playbackState = PlaybackStateCompat.STATE_PLAYING;
            playPauseAction.setIndex(PlayPauseAction.PAUSE);
            setFadingEnabled(true);
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            startProgressAutomation();
        } else if (initialised) {
            playbackState = PlaybackStateCompat.STATE_PAUSED;
            playPauseAction.setIndex(PlayPauseAction.PLAY);
        }

        rowsAdapter.notifyArrayItemRangeChanged(rowsAdapter.indexOf(playbackControlsRow), 1);

        break;

    default:
        break;
    }

    updateButtonVisibilities();

    if (callback != null) {
        callback.onPlaybackStatusChanged(playbackState);
    }
}