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

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

Introduction

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

Prototype

int STATE_PLAYING

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

Click Source Link

Document

State indicating this item is currently playing.

Usage

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:/*from ww  w.j a v  a 2s. co  m*/
        return PlaybackStateCompat.STATE_NONE;
    }
}

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

private void configMediaPlayerState() {
    String state = "?";
    int oldState = playbackState;

    if (mediaPlayer != null && !mediaPlayer.getPlayWhenReady()) {
        mediaPlayer.setPlayWhenReady(true);

        if (currentPosition == mediaPlayer.getCurrentPosition()) {
            playbackState = PlaybackStateCompat.STATE_PLAYING;

            state = "Playing";
        } else {//ww  w.  jav a 2s.  co m
            seekTo(currentPosition);

            state = "Seeking to " + currentPosition;
        }
    }

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

    Log.d(TAG, "configMediaPlayerState() > " + state);
}

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.
 *///from  w  w  w  .j  a  v  a  2s.c om
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 play() {
    // Only when player is not null (meaning the player has been created), the player is
    // prepared (using the mInitialized as the flag to represent it,
    // this boolean variable will only be assigned to true inside of the onPrepared callback)
    // and the media item is not currently playing (!isPlaying()), then the player can be
    // started./*from  w  w  w  .j  a  v a  2  s.c  o m*/

    // If the player has not been prepared, but this function is fired, it is an error state
    // from the app side
    if (!mInitialized) {
        PlaybackStateCompat.Builder builder = createPlaybackStateBuilder(PlaybackStateCompat.STATE_ERROR);
        builder.setErrorMessage(PlaybackStateCompat.ERROR_CODE_APP_ERROR, PLAYER_NOT_INITIALIZED);
        mMediaSession.setPlaybackState(builder.build());

        // If the player has is playing, and this function is fired again, it is an error state
        // from the app side
    } else {
        // Request audio focus only when needed
        if (mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN) != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            return;
        }

        if (mPlayer.getPlaybackParams().getSpeed() != NORMAL_SPEED) {
            // Reset to normal speed and play
            resetSpeedAndPlay();
        } else {
            // Continue play.
            mPlayer.start();
            mMediaSession
                    .setPlaybackState(createPlaybackStateBuilder(PlaybackStateCompat.STATE_PLAYING).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.
 *///from  www. jav a 2  s  .  co m
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   w  ww  .  j  a v  a 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);
    }
}

From source file:com.torrenttunes.android.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.ja v  a 2  s .  com*/
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.achep.acdisplay.ui.fragments.AcDisplayFragment.java

@Override
public void onPlaybackStateChanged(int state) {
    switch (state) {
    case PlaybackStateCompat.STATE_PLAYING:
        mHandler.removeMessages(MSG_HIDE_MEDIA_WIDGET);
        makeMediaWidgetActive();//from ww w  .  ja v a  2s. co m
        break;
    default:
        if (mMediaWidgetActive) {
            int delay = 6000; // 6 sec.
            if (state == PlaybackStateCompat.STATE_NONE)
                delay = 500;
            mHandler.sendEmptyMessageDelayed(MSG_HIDE_MEDIA_WIDGET, delay);
        }
        break;
    }
}

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

private void resetSpeedAndPlay() {

    if (mIsRewindBegin) {
        mIsRewindBegin = false;//from   ww w. j  av a 2s . c o  m
        mRewindEndTime = SystemClock.elapsedRealtime();

        long position = mRewindStartPosition
                + (long) mRewindSpeedFactors[mRewindSpeedFactorIndex] * (mRewindEndTime - mRewindStartTime);

        // Seek to the computed position for seamless playing.
        mPlayer.seekTo((int) position);
    }

    // Reset the state to normal state.
    mFastForwardSpeedFactorIndex = 0;
    mRewindSpeedFactorIndex = 0;
    mPlayer.setPlaybackParams(
            mPlayer.getPlaybackParams().setSpeed(mFastForwardSpeedFactors[mFastForwardSpeedFactorIndex]));

    // Update the playback status from rewinding/ fast forwardindg to STATE_PLAYING.
    // Which indicates current media item is played in the normal speed.
    mMediaSession.setPlaybackState(createPlaybackStateBuilder(PlaybackStateCompat.STATE_PLAYING).build());
}

From source file:dk.nota.lyt.libvlc.PlaybackService.java

protected void publishState(int state) {
    if (mMediaSession == null)
        return;// ww w.  j a va  2s .c om
    PlaybackStateCompat.Builder bob = new PlaybackStateCompat.Builder();
    bob.setActions(PLAYBACK_ACTIONS);
    switch (state) {
    case MediaPlayer.Event.Playing:
        bob.setState(PlaybackStateCompat.STATE_PLAYING, getCurrentMediaPosition(), getRate());
        break;
    case MediaPlayer.Event.Stopped:
        bob.setState(PlaybackStateCompat.STATE_STOPPED, getCurrentMediaPosition(), getRate());
        break;
    default:
        bob.setState(PlaybackStateCompat.STATE_PAUSED, getCurrentMediaPosition(), getRate());
    }
    PlaybackStateCompat pbState = bob.build();
    mMediaSession.setPlaybackState(pbState);
    mMediaSession.setActive(state != PlaybackStateCompat.STATE_STOPPED);
}