public MediaControllerCompatAssert hasPosition(long position) { isNotNull(); long actualPosition = actual.getPlaybackState().getPosition(); assertThat(actualPosition) // .overridingErrorMessage("Expected position <%s> but was <%s>.", position, actualPosition) // .isEqualTo(position); return this; }
@Override public void onPlaybackStateChanged(PlaybackStateCompat state) { super.onPlaybackStateChanged(state); // If there's an ongoing animation, stop it now. if (mProgressAnimator != null) { mProgressAnimator.cancel(); mProgressAnimator = null; } final int progress = state != null ? (int) state.getPosition() : 0; setProgress(progress); // If the media is playing then the seekbar should follow it, and the easiest // way to do that is to create a ValueAnimator to update it so the bar reaches // the end of the media the same time as playback gets there (or close enough). if (state != null && state.getState() == PlaybackStateCompat.STATE_PLAYING) { final int timeToEnd = (int) ((getMax() - progress) / state.getPlaybackSpeed()); mProgressAnimator = ValueAnimator.ofInt(progress, getMax()) .setDuration(timeToEnd); mProgressAnimator.setInterpolator(new LinearInterpolator()); mProgressAnimator.addUpdateListener(this); mProgressAnimator.start(); } }
@Override 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)); } });
mPlaybackState.getPosition() >= 0 && mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) { Log.d(TAG, "updateNotificationPlaybackState. updating playback position to " + (System.currentTimeMillis() - mPlaybackState.getPosition()) / 1000 + " seconds"); mNotificationBuilder .setWhen(System.currentTimeMillis() - mPlaybackState.getPosition()) .setShowWhen(true) .setUsesChronometer(true);
private void setNotificationPlaybackState(NotificationCompat.Builder builder) { if (mPlaybackState == null || !mStarted) { mService.stopForeground(true); 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); } } }
@Override public void updateProgress(PlaybackStateCompat playbackState, long duration) { if (playbackState != null && mTomahawkListAdapter != null && mTomahawkListAdapter.getProgressBar() != null) { 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(); } mTomahawkListAdapter.getProgressBar().setProgress( (int) ((float) currentPosition / duration * mTomahawkListAdapter.getProgressBar().getMax())); } } });