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:net.simno.klingar.MediaNotificationManager.java

private void setNotificationPlaybackState(NotificationCompat.Builder builder) {
    PlaybackStateCompat playbackState = musicController.getPlaybackState();
    if (playbackState == null || !started) {
        service.stopForeground(true);/*from w  w  w .j  av a  2 s.c  om*/
        return;
    }
    if (playbackState.getState() == PlaybackStateCompat.STATE_PLAYING && playbackState.getPosition() >= 0) {
        builder.setWhen(System.currentTimeMillis() - playbackState.getPosition()).setShowWhen(true)
                .setUsesChronometer(true);
    } else {
        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(playbackState.getState() == PlaybackStateCompat.STATE_PLAYING);
}

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

private void setupRows() {
    PlaybackControlsRowPresenter playbackControlsRowPresenter;
    playbackControlsRowPresenter = new PlaybackControlsRowPresenter(new DescriptionPresenter());

    playbackControlsRowPresenter.setOnActionClickedListener(new OnActionClickedListener() {
        public void onActionClicked(Action action) {
            if (action.getId() == playPauseAction.getId()) {
                if (lastPlaybackState != null) {
                    if (lastPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
                        mediaController.getTransportControls().pause();
                    } else {
                        mediaController.getTransportControls().play();
                    }/*from   w  w w. j  av  a 2  s . com*/
                }
            } else if (action.getId() == skipNextAction.getId()) {
                mediaController.getTransportControls().skipToNext();
            } else if (action.getId() == skipPreviousAction.getId()) {
                mediaController.getTransportControls().skipToPrevious();
            } else if (action.getId() == stopAction.getId()) {
                mediaController.getTransportControls().stop();
            } else if (action.getId() == clearPlaylistAction.getId()) {
                Log.d(TAG, "onActionClicked -> Clear Playlist");
                MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();
                controls.sendCustomAction(MediaService.ACTION_CLEAR_PLAYLIST, null);
            } else if (action.getId() == shuffleAction.getId()) {
                Log.d(TAG, "onActionClicked -> Shuffle");

                PlaybackStateCompat state = mediaController.getPlaybackState();
                MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();

                if (state == null) {
                    return;
                }

                for (PlaybackStateCompat.CustomAction cAction : state.getCustomActions()) {
                    switch (cAction.getAction()) {
                    case MediaService.STATE_SHUFFLE_ON:
                        controls.sendCustomAction(MediaService.STATE_SHUFFLE_ON, null);
                        break;

                    case MediaService.STATE_SHUFFLE_OFF:
                        controls.sendCustomAction(MediaService.STATE_SHUFFLE_OFF, null);
                        break;
                    }
                }

                updatePlaybackState(state);
            } else if (action.getId() == repeatAction.getId()) {
                Log.d(TAG, "onActionClicked -> Repeat");

                PlaybackStateCompat state = mediaController.getPlaybackState();
                MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();

                if (state == null) {
                    return;
                }

                for (PlaybackStateCompat.CustomAction cAction : state.getCustomActions()) {
                    switch (cAction.getAction()) {
                    case MediaService.STATE_REPEAT_NONE:
                    case MediaService.STATE_REPEAT_ALL:
                    case MediaService.STATE_REPEAT_ONE:
                        controls.sendCustomAction(cAction.getAction(), null);
                        break;
                    }
                }

                updatePlaybackState(state);
            }
        }
    });

    presenterSelector.addClassPresenter(PlaybackControlsRow.class, playbackControlsRowPresenter);
    presenterSelector.addClassPresenter(String.class, new HeaderPresenter());

    playbackControlsRow = new PlaybackControlsRow(new MediaDescriptionHolder());
    ControlButtonPresenterSelector presenterSelector = new ControlButtonPresenterSelector();
    primaryActionsAdapter = new ArrayObjectAdapter(presenterSelector);
    secondaryActionsAdapter = new ArrayObjectAdapter(presenterSelector);
    playbackControlsRow.setPrimaryActionsAdapter(primaryActionsAdapter);
    playbackControlsRow.setSecondaryActionsAdapter(secondaryActionsAdapter);

    playPauseAction = new PlayPauseAction(getActivity());
    stopAction = new StopAction((getActivity()));
    skipNextAction = new PlaybackControlsRow.SkipNextAction(getActivity());
    skipPreviousAction = new PlaybackControlsRow.SkipPreviousAction(getActivity());
    clearPlaylistAction = new ClearPlaylistAction(getActivity());
    shuffleAction = new PlaybackControlsRow.ShuffleAction(getActivity());
    repeatAction = new PlaybackControlsRow.RepeatAction(getActivity());

    primaryActionsAdapter.add(skipPreviousAction);
    primaryActionsAdapter.add(stopAction);
    primaryActionsAdapter.add(playPauseAction);
    primaryActionsAdapter.add(skipNextAction);

    secondaryActionsAdapter.add(shuffleAction);
    secondaryActionsAdapter.add(repeatAction);
    secondaryActionsAdapter.add(clearPlaylistAction);

    rowsAdapter.add(playbackControlsRow);

    // Add heading
    rowsAdapter.add(getString(R.string.heading_queue));

    setAdapter(rowsAdapter);
    setOnItemViewClickedListener(new ItemViewClickedListener());
}

From source file:com.torrenttunes.android.CastPlayback.java

private void updatePlaybackState() {
    int status = mCastManager.getPlaybackStatus();
    int idleReason = mCastManager.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  w  ww  . ja 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;
        updateMetadata();
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
        break;
    case MediaStatus.PLAYER_STATE_PAUSED:
        mState = PlaybackStateCompat.STATE_PAUSED;
        updateMetadata();
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
        break;
    default: // case unknown
        LogHelper.d(TAG, "State default : ", status);
        break;
    }
}

From source file:com.example.android.uamp.playback.LocalPlayback.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./*w w  w  . j  a v a  2 s  .  c  o  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) {
            mMediaPlayer.setVolume(VOLUME_DUCK, VOLUME_DUCK); // we'll be relatively quiet
        } else {
            if (mMediaPlayer != null) {
                mMediaPlayer.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 (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
                LogHelper.d(TAG, "configMediaPlayerState startMediaPlayer. seeking to ", mCurrentPosition);
                if (mCurrentPosition == mMediaPlayer.getCurrentPosition()) {
                    mMediaPlayer.start();
                    mState = PlaybackStateCompat.STATE_PLAYING;
                } else {
                    mMediaPlayer.seekTo(mCurrentPosition);
                    mState = PlaybackStateCompat.STATE_BUFFERING;
                }
            }
            mPlayOnFocusGain = false;
        }
    }
    if (mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
}

From source file:org.amahi.anywhere.util.MediaNotificationManager.java

private void addPlayPauseAction(NotificationCompat.Builder builder) {
    Log.d(TAG, "updatePlayPauseAction");
    String label;/*from   w  w  w  . j  a  v a2 s  .  c o m*/
    int icon;
    PendingIntent intent;
    if (mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
        label = mService.getString(R.string.label_pause);
        icon = android.R.drawable.ic_media_pause;
        intent = mPauseIntent;
    } else {
        label = mService.getString(R.string.label_play);
        icon = android.R.drawable.ic_media_play;
        intent = mPlayIntent;
    }
    builder.addAction(new NotificationCompat.Action(icon, label, intent));
}

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

private void addPlayPauseAction(NotificationCompat.Builder builder) {
    Log.d(TAG, "updatePlayPauseAction");
    String label;/*from w  w w .java 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:com.scooter1556.sms.android.activity.FullScreenPlayerActivity.java

private void connectToSession(MediaSessionCompat.Token token) throws RemoteException {
    mediaController = new MediaControllerCompat(FullScreenPlayerActivity.this, token);

    if (mediaController.getMetadata() == null) {
        finish();//  w w w  .j a  v  a2s.  co  m
        return;
    }

    MediaControllerCompat.setMediaController(this, mediaController);
    mediaController.registerCallback(callback);
    PlaybackStateCompat state = mediaController.getPlaybackState();
    updatePlaybackState(state);
    MediaMetadataCompat metadata = mediaController.getMetadata();

    if (metadata != null) {
        updateMediaDescription(metadata.getDescription());
        updateDuration(metadata);
    }

    updateProgress();

    if (state != null && (state.getState() == PlaybackStateCompat.STATE_PLAYING
            || state.getState() == PlaybackStateCompat.STATE_BUFFERING)) {
        scheduleSeekbarUpdate();
    }
}

From source file:com.murati.oszk.audiobook.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  ava  2s .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 = MediaControllerCompat.getMediaController(getActivity());
    updatePlayListRow(controller.getQueue());
    mRowsAdapter.notifyArrayItemRangeChanged(mRowsAdapter.indexOf(mPlaybackControlsRow), 1);
}

From source file:org.amahi.anywhere.util.MediaNotificationManager.java

private void setNotificationPlaybackState(NotificationCompat.Builder builder) {
    if (mPlaybackState == null || !mStarted) {
        mService.stopForeground(true);//from  w  w w .  j a v  a 2  s.  c  om
        return;
    }
    if (mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING && mPlaybackState.getPosition() >= 0) {
        builder.setWhen(System.currentTimeMillis() - mPlaybackState.getPosition()).setShowWhen(true)
                .setUsesChronometer(true);
    } else {
        builder.setWhen(0).setShowWhen(false).setUsesChronometer(false);
    }
}

From source file:com.classiqo.nativeandroid_32bitz.MediaNotificationManager.java

private void addPlayPauseAction(NotificationCompat.Builder builder) {
    LogHelper.d(TAG, "updatePlayPauseAction");
    String label;//from  w ww. ja v  a 2s  .co  m
    int icon;
    PendingIntent intent;

    if (mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
        label = mService.getString(R.string.label_pause);
        icon = R.drawable.uamp_ic_pause_white_24dp;
        intent = mPauseIntent;
    } else {
        label = mService.getString(R.string.label_play);
        icon = R.drawable.uamp_ic_play_arrow_white_24dp;
        intent = mPlayIntent;
    }
    builder.addAction(new NotificationCompat.Action(icon, label, intent));
}