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.example.chu.googleplaylibrary.playback.CastPlayback.java

private void updatePlaybackState() {
    int status = VideoCastManager.getInstance().getPlaybackStatus();
    int idleReason = VideoCastManager.getInstance().getIdleReason();

    LogHelper.d(TAG, "onRemoteMediaPlayerStatusUpdated ", status);

    // Convert the remote playback states to media playback states.
    switch (status) {
    case MediaStatus.PLAYER_STATE_IDLE:
        if (idleReason == MediaStatus.IDLE_REASON_FINISHED) {
            if (mCallback != null) {
                mCallback.onCompletion();
            }//from www  .  j a v a2s. c  om
        }
        break;
    case MediaStatus.PLAYER_STATE_BUFFERING:
        mState = PlaybackStateCompat.STATE_BUFFERING;
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
        break;
    case MediaStatus.PLAYER_STATE_PLAYING:
        mState = PlaybackStateCompat.STATE_PLAYING;
        setMetadataFromRemote();
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
        break;
    case MediaStatus.PLAYER_STATE_PAUSED:
        mState = PlaybackStateCompat.STATE_PAUSED;
        setMetadataFromRemote();
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
        break;
    default: // case unknown
        LogHelper.d(TAG, "State default : ", status);
        break;
    }
}

From source file:net.simno.klingar.playback.LocalPlayback.java

/**
 * Reconfigures ExoPlayer according to audio focus settings and starts/restarts it.
 * This method starts/restarts the ExoPlayer respecting the current audio focus state.
 * So if we have focus, it will play normally; if we don't have focus, it will either leave
 * ExoPlayer paused or set it to a low volume, depending on what is  allowed by the current
 * focus settings. This method assumes exoPlayer != null, so if you are calling it,
 * you have to do so from a context where you are sure this is the case.
 *//*from w w  w  .j  ava  2  s.c  o  m*/
private void configExoPlayerState() {
    Timber.d("configExoPlayerState audioFocus %s", audioFocus);
    if (audioFocus == AUDIO_NO_FOCUS_NO_DUCK) {
        // If we don't have audio focus and can't duck, we have to pause,
        if (state == PlaybackStateCompat.STATE_PLAYING) {
            pause();
        }
    } else { // we have audio focus
        registerAudioNoisyReceiver();
        if (audioFocus == AUDIO_NO_FOCUS_CAN_DUCK) {
            if (exoPlayer != null) {
                exoPlayer.setVolume(VOLUME_DUCK); // we'll be relatively quiet
            }
        } else {
            if (exoPlayer != null) {
                exoPlayer.setVolume(VOLUME_NORMAL); // we can be loud again
            }
        }
        // If we were playing when we lost focus, we need to resume playing.
        if (playOnFocusGain) {
            if (exoPlayer != null && !exoPlayer.getPlayWhenReady()) {
                Timber.d("configExoPlayerState seeking to %s", currentPosition);
                if (currentPosition == exoPlayer.getCurrentPosition()) {
                    state = PlaybackStateCompat.STATE_PLAYING;
                    exoPlayer.setPlayWhenReady(true);
                } else {
                    state = PlaybackStateCompat.STATE_BUFFERING;
                    seekTo((int) currentPosition);
                    exoPlayer.setPlayWhenReady(true);
                }
                if (callback != null) {
                    callback.onPlaybackStatusChanged();
                }
            }
            playOnFocusGain = false;
        }
    }
}

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

private void addPlayPauseAction(NotificationCompat.Builder builder) {
    LogHelper.d(TAG, "updatePlayPauseAction");
    String label;/*  w  ww .j  av  a 2 s.c o  m*/
    int icon;
    PendingIntent intent;
    if (mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
        label = mService.getString(R.string.label_pause);
        icon = R.drawable.ic_pause_white_24dp;
        intent = mPauseIntent;
    } else {
        label = mService.getString(R.string.label_play);
        icon = R.drawable.ic_play_arrow_white_24dp;
        intent = mPlayIntent;
    }
    builder.addAction(new NotificationCompat.Action(icon, label, intent));
}

From source file:rocks.stalin.android.app.playback.RemotePlayback.java

/**
 * Reconfigures MediaPlayer according to audio focus settings and
 * starts/restarts it. This method starts/restarts the MediaPlayer
 * respecting the current audio focus state. So if we have focus, it will
 * play normally; if we don't have focus, it will either leave the
 * MediaPlayer paused or set it to a low volume, depending on what is
 * allowed by the current focus settings. This method assumes mPlayer !=
 * null, so if you are calling it, you have to do so from a context where
 * you are sure this is the case./* www. j  a  v  a2  s.  co m*/
 */
private void configMediaPlayerState() {
    LogHelper.d(TAG, "configMediaPlayerState. mAudioFocus=", mAudioFocus);
    if (mAudioFocus == AUDIO_NO_FOCUS_NO_DUCK) {
        // If we don't have audio focus and can't duck, we have to pause,
        if (mState == PlaybackStateCompat.STATE_PLAYING) {
            pause();
        }
    } else { // we have audio focus:
        registerAudioNoisyReceiver();
        if (mAudioFocus == AUDIO_NO_FOCUS_CAN_DUCK) {
            mediaPlayer.setVolume(VOLUME_DUCK, VOLUME_DUCK); // we'll be relatively quiet
        } else {
            if (mediaPlayer != null) {
                mediaPlayer.setVolume(VOLUME_NORMAL, VOLUME_NORMAL); // we can be loud again
            } // else do something for remote client.
        }
        // If we were playing when we lost focus, we need to resume playing.
        if (mPlayOnFocusGain) {
            if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
                LogHelper.d(TAG, "configMediaPlayerState startMediaPlayer. seeking to ", mCurrentPosition);
                if (mCurrentPosition == mediaPlayer.getCurrentPosition()) {
                    mediaPlayer.start();
                    mState = PlaybackStateCompat.STATE_PLAYING;
                } else {
                    mediaPlayer.seekTo(mCurrentPosition);
                    mState = PlaybackStateCompat.STATE_BUFFERING;
                }
            }
            mPlayOnFocusGain = false;
        }
    }
    if (mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
}

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

private void setNotificationPlaybackState(NotificationCompat.Builder builder) {
    Log.d(TAG, "updateNotificationPlaybackState. mPlaybackState=" + mPlaybackState);
    if (mPlaybackState == null || !mStarted) {
        Log.d(TAG, "updateNotificationPlaybackState. cancelling notification!");
        mService.stopForeground(true);//ww w.  j  a v a2 s. c o m
        return;
    }
    if (mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING && mPlaybackState.getPosition() >= 0) {
        Log.d(TAG, "updateNotificationPlaybackState. updating playback position to "
                + (System.currentTimeMillis() - mPlaybackState.getPosition()) / 1000 + " seconds");
        builder.setWhen(System.currentTimeMillis() - mPlaybackState.getPosition()).setShowWhen(true)
                .setUsesChronometer(true);
    } else {
        Log.d(TAG, "updateNotificationPlaybackState. hiding playback position");
        builder.setWhen(0).setShowWhen(false).setUsesChronometer(false);
    }

    // Make sure that the notification can be dismissed by the user when we are not playing:
    builder.setOngoing(mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING);
}

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

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link AudioManager.OnAudioFocusChangeListener}
 *//*from w ww .ja  va 2s.c om*/
@Override
public void onAudioFocusChange(int focusChange) {
    if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        // We have gained focus:
        mAudioFocus = AUDIO_FOCUSED;

    } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS
            || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
            || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
        // We have lost focus. If we can duck (low playback volume), we can keep playing.
        // Otherwise, we need to pause the playback.
        boolean canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
        mAudioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK;

        // If we are playing, we need to reset media player by calling configMediaPlayerState
        // with mAudioFocus properly set.
        if (mState == PlaybackStateCompat.STATE_PLAYING && !canDuck) {
            // If we don't have audio focus and can't duck, we save the information that
            // we were playing, so that we can resume playback once we get the focus back.
            mPlayOnFocusGain = true;
        }
    } else {
        Log.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: " + focusChange);
    }
    configMediaPlayerState();
}

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

private void setupRows() {
    ClassPresenterSelector presenter = new ClassPresenterSelector();

    PlaybackControlsRowPresenter playbackControlsRowPresenter;
    playbackControlsRowPresenter = new PlaybackControlsRowPresenter(new DescriptionPresenter());

    playbackControlsRowPresenter.setOnActionClickedListener(new OnActionClickedListener() {
        public void onActionClicked(Action action) {
            if (action.getId() == playPauseAction.getId()) {
                if (playbackState == PlaybackStateCompat.STATE_PLAYING) {
                    pause();/*from   ww  w .j a v  a 2  s . c o m*/
                } else {
                    play(currentMedia);
                }
            } else if (action.getId() == fastForwardAction.getId()) {
                if (seekInProgress) {
                    if (seekSpeed > 0) {
                        seekSpeed = seekSpeed * 2;
                        if (seekSpeed > MAX_SEEK_SPEED) {
                            seekSpeed = MAX_SEEK_SPEED;
                        }
                    } else {
                        seekSpeed = DEFAULT_SEEK_SPEED;
                    }
                } else {
                    seekSpeed = DEFAULT_SEEK_SPEED;
                    startSeeking();
                }
            } else if (action.getId() == rewindAction.getId()) {
                if (seekInProgress) {
                    if (seekSpeed < 0) {
                        seekSpeed = seekSpeed * 2;
                        if (seekSpeed < (MAX_SEEK_SPEED * -1)) {
                            seekSpeed = (MAX_SEEK_SPEED * -1);
                        }
                    } else {
                        seekSpeed = (DEFAULT_SEEK_SPEED * -1);
                    }
                } else {
                    seekSpeed = (DEFAULT_SEEK_SPEED * -1);
                    startSeeking();
                }
            } else if (action.getId() == videoQualityAction.getId()) {
                MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector
                        .getCurrentMappedTrackInfo();

                if (mappedTrackInfo != null) {
                    trackSelectionUtils.showSelectionDialog(getActivity(),
                            trackSelector.getCurrentMappedTrackInfo(), videoQualityAction.getIndex());
                }
            } else if (action.getId() == audioTrackAction.getId()) {
                MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector
                        .getCurrentMappedTrackInfo();

                if (mappedTrackInfo != null) {
                    trackSelectionUtils.showSelectionDialog(getActivity(),
                            trackSelector.getCurrentMappedTrackInfo(), audioTrackAction.getIndex());
                }
            } else if (action.getId() == subtitleTrackAction.getId()) {
                MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector
                        .getCurrentMappedTrackInfo();

                if (mappedTrackInfo != null) {
                    trackSelectionUtils.showSelectionDialog(getActivity(),
                            trackSelector.getCurrentMappedTrackInfo(), subtitleTrackAction.getIndex());
                }
            }
        }
    });

    presenter.addClassPresenter(PlaybackControlsRow.class, playbackControlsRowPresenter);
    presenter.addClassPresenter(ListRow.class, new ListRowPresenter());
    rowsAdapter = new ArrayObjectAdapter(presenter);

    addPlaybackControlsRow();

    setAdapter(rowsAdapter);
}

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

/**
 * Switch to a different Playback instance, maintaining all playback state, if possible.
 *
 * @param playback switch to this playback
 *//*  w  ww . jav a 2s. co m*/
public void switchToPlayback(Playback playback, boolean resumePlaying) {
    if (playback == null) {
        throw new IllegalArgumentException("Playback cannot be null");
    }
    playback.setSurface(mPlayback.getSurfaceId(), mPlayback.getSurface());
    // suspend the current one.
    final int oldState = mPlayback.getState();
    long pos = mPlayback.getCurrentStreamPosition();
    if (pos < 0)
        pos = mPlayback.getStartStreamPosition();
    mPlayback.stop(false);
    playback.setCallback(this);
    playback.setCurrentStreamPosition(pos);
    playback.setCurrentMediaMetadata(mPlayback.getCurrentMediaId(), mPlayback.getCurrentMetadata());
    playback.start();
    // finally swap the instance
    mPlayback = playback;
    switch (oldState) {
    case PlaybackStateCompat.STATE_BUFFERING:
    case PlaybackStateCompat.STATE_CONNECTING:
    case PlaybackStateCompat.STATE_PAUSED:
        mPlayback.pause();
        break;
    case PlaybackStateCompat.STATE_PLAYING:
        if (resumePlaying && mMediaMetadata != null) {
            mPlayback.play(mMediaMetadata);
        } else if (!resumePlaying) {
            mPlayback.pause();
        } else {
            mPlayback.stop(true);
        }
        break;
    case PlaybackStateCompat.STATE_NONE:
        break;
    default:

    }
}

From source file:com.bayapps.android.robophish.ui.tv.TvPlaybackFragment.java

protected void updatePlaybackState(PlaybackStateCompat state) {
    if (mPlaybackControlsRow == null) {
        // We only update playback state after we get a valid metadata.
        return;// w  w  w. j a v a2 s . c om
    }
    mLastPosition = state.getPosition();
    mLastPositionUpdateTime = state.getLastPositionUpdateTime();
    switch (state.getState()) {
    case PlaybackStateCompat.STATE_PLAYING:
        startProgressAutomation();
        mPlayPauseAction.setIndex(PlayPauseAction.PAUSE);
        break;
    case PlaybackStateCompat.STATE_PAUSED:
        stopProgressAutomation();
        mPlayPauseAction.setIndex(PlayPauseAction.PLAY);
        break;
    }

    MediaControllerCompat controller = getActivity().getSupportMediaController();
    updatePlayListRow(controller.getQueue());
    mRowsAdapter.notifyArrayItemRangeChanged(mRowsAdapter.indexOf(mPlaybackControlsRow), 1);
}

From source file:com.scooter1556.sms.android.playback.CastPlayback.java

private void updatePlaybackState() {
    String log = "updatePlaybackState() > ";

    int status = remoteMediaClient.getPlayerState();
    int idleReason = remoteMediaClient.getIdleReason();

    // Convert the remote playback states to media playback states
    switch (status) {
    case MediaStatus.PLAYER_STATE_IDLE:
        log += "IDLE";

        if (idleReason == MediaStatus.IDLE_REASON_FINISHED && !finished) {
            log += ":FINISHED";

            finished = true;//from w w w  .j  a va  2  s .  com
            currentJobId = null;

            if (callback != null) {
                callback.onCompletion();
            }
        }

        break;

    case MediaStatus.PLAYER_STATE_BUFFERING:
        log += "BUFFERING";

        finished = false;

        playbackState = PlaybackStateCompat.STATE_BUFFERING;

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

        break;

    case MediaStatus.PLAYER_STATE_PLAYING:
        log += "PLAYING";

        finished = false;

        playbackState = PlaybackStateCompat.STATE_PLAYING;
        setMetadataFromRemote();

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

        break;

    case MediaStatus.PLAYER_STATE_PAUSED:
        log += "PAUSED";

        playbackState = PlaybackStateCompat.STATE_PAUSED;
        setMetadataFromRemote();

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

    default:
        log += "UNKNOWN:" + status;
        break;
    }

    Log.d(TAG, log);
}