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

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

Introduction

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

Prototype

public long getLastPositionUpdateTime() 

Source Link

Document

Get the elapsed real time at which position was last updated.

Usage

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;/*ww  w. j a v  a  2s. co m*/
    }
    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: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;/*from   ww w  . j a v a2  s. c o m*/
    }
    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:org.runbuddy.tomahawk.views.PlaybackPanel.java

private void init() {
    mTextViewContainer = (FrameLayout) findViewById(R.id.textview_container);
    mPanelContainer = findViewById(R.id.panel_container);
    mStationContainer = findViewById(R.id.station_container);
    mStationContainerInner = findViewById(R.id.station_container_inner);
    mStationContainerInner.addOnLayoutChangeListener(mStationLayoutChangeListener);
    mArtistNameButton = (FrameLayout) mTextViewContainer.findViewById(R.id.artist_name_button);
    mArtistTextView = (TextView) mArtistNameButton.findViewById(R.id.artist_textview);
    mTrackTextView = (TextView) mTextViewContainer.findViewById(R.id.track_textview);
    mCompletionTimeTextView = (TextView) findViewById(R.id.completiontime_textview);
    mCurrentTimeTextView = (TextView) findViewById(R.id.currenttime_textview);
    mSeekTimeTextView = (TextView) findViewById(R.id.seektime_textview);
    mStationTextView = (TextView) findViewById(R.id.station_textview);
    mResolverImageView = (ImageView) findViewById(R.id.resolver_imageview);
    mPlayButton = (ImageView) findViewById(R.id.play_button);
    mPauseButton = (ImageView) findViewById(R.id.pause_button);
    mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
    mProgressBarThumb = findViewById(R.id.progressbar_thumb);
    mPlayPauseButtonContainer = (FrameLayout) findViewById(R.id.circularprogressbar_container);
    mPlayPauseButton = (CircularProgressView) mPlayPauseButtonContainer.findViewById(R.id.circularprogressbar);

    mProgressBarUpdater = new ProgressBarUpdater(new ProgressBarUpdater.UpdateProgressRunnable() {
        @Override//from  w ww  . j  a  va 2  s. co m
        public void updateProgress(PlaybackStateCompat playbackState, long duration) {
            if (playbackState == null) {
                return;
            }
            long currentPosition = playbackState.getPosition();
            if (playbackState.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 ensure that we do not repeatedly call the getPlaybackState()
                // on MediaControllerCompat.
                long timeDelta = SystemClock.elapsedRealtime() - playbackState.getLastPositionUpdateTime();
                currentPosition += (int) timeDelta * playbackState.getPlaybackSpeed();
            }
            mProgressBar.setProgress((int) ((float) currentPosition / duration * 10000));
            mPlayPauseButton.setProgress((float) currentPosition / duration * 1000);
            mCurrentTimeTextView.setText(ViewUtils.durationToString(currentPosition));
        }
    });
}

From source file:nuclei.media.MediaPlayerController.java

@Override
public int getCurrentPosition() {
    if (isEquals()) {
        PlaybackStateCompat state = mMediaControls.getPlaybackState();
        if (state != null) {
            // KJB: NOTE: Pulled from media compat library
            //            For some reason, it seems, that only API 21 doesn't do something equivalent
            //            of this.  weird.
            //            Just to be safe, since this is important, doing it here.
            //            It's a little redundant... but, I don't see how this would hurt anything
            //            if it were executed twice (so long as getLastPositionUpdateTime is correct)
            //      TODO: revisit this after support library 25.1.1
            try {
                if ((state.getState() == PlaybackStateCompat.STATE_PLAYING
                        || state.getState() == PlaybackStateCompat.STATE_FAST_FORWARDING
                        || state.getState() == PlaybackStateCompat.STATE_REWINDING)) {
                    final long updateTime = state.getLastPositionUpdateTime();
                    final long currentTime = SystemClock.elapsedRealtime();
                    if (updateTime > 0) {
                        final float speed = state.getPlaybackSpeed();
                        final long duration = getDuration();
                        long position = (long) (speed * (currentTime - updateTime)) + state.getPosition();
                        if (duration >= 0 && position > duration) {
                            position = duration;
                        } else if (position < 0) {
                            position = 0;
                        }//from   w  ww  . ja  va  2  s  .  c om
                        return (int) position;
                    }
                }
            } catch (Exception err) { // because weird things happen sometimes :(
                LOG.e("Error calculating latest position", err);
            }
            return (int) state.getPosition();
        }
    }
    return -1;
}