Skip to content

Commit

Permalink
Extract time bar delay calculations to a static method
Browse files Browse the repository at this point in the history
  • Loading branch information
szaboa committed Feb 16, 2019
1 parent ecfce46 commit eaf0d40
Showing 1 changed file with 53 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -837,36 +837,7 @@ private void updateProgress() {
if (player.getPlayWhenReady() && playbackState == Player.STATE_READY) {
float playbackSpeed = player.getPlaybackParameters().speed;
int timeBarWidth = timeBar.getTimeBarWidthDp();

if (timeBarWidth == 0 || duration == 0) {
delayMs = MAX_UPDATE_FREQUENCY_MS;
} else {
// Calculate how many updates needs to be done with DEFAULT_UPDATE_DP
// to fill up the time bar
int numberOfUpdates = timeBarWidth / DEFAULT_UPDATE_DP;

// Calculate the designated update interval, taking duration into consideration as well
long mediaTimeUpdatePeriodMs = duration / numberOfUpdates;

// Calculate the delay needed from the current position until the next update is due
long mediaTimeDelayMs = mediaTimeUpdatePeriodMs - (position % mediaTimeUpdatePeriodMs);

// If the delay would be too small, then skip the next update
if (mediaTimeDelayMs < (mediaTimeUpdatePeriodMs / 5)) {
mediaTimeDelayMs += mediaTimeUpdatePeriodMs;
}

// Calculate the delay until the next update (in real time), taking
// playbackSpeed into consideration
delayMs = playbackSpeed == 1 ? mediaTimeDelayMs : (long) (mediaTimeDelayMs / playbackSpeed);

// Limit the delay if needed, to avoid too frequent / infrequent updates
if (delayMs < MIN_UPDATE_FREQUENCY_MS) {
delayMs = MIN_UPDATE_FREQUENCY_MS;
} else if (delayMs >= MAX_UPDATE_FREQUENCY_MS) {
delayMs = MAX_UPDATE_FREQUENCY_MS;
}
}
delayMs = getTimeBarUpdateDelayMs(timeBarWidth, position, duration, playbackSpeed);
} else {
delayMs = MAX_UPDATE_FREQUENCY_MS;
}
Expand Down Expand Up @@ -1097,6 +1068,58 @@ private static boolean canShowMultiWindowTimeBar(Timeline timeline, Timeline.Win
return true;
}

/**
* Calculates the update delay of time bar in millis
*
* @param timeBarWidth The width of time bar in dp
* @param position The current position in millis
* @param duration The duration of media in millis
* @param playbackSpeed The playback speed
* @return Update delay of time bar in millis
*/
private static long getTimeBarUpdateDelayMs(int timeBarWidth, long position,
long duration, float playbackSpeed) {
if (timeBarWidth == 0 || duration == 0 || playbackSpeed == 0) {
return MAX_UPDATE_FREQUENCY_MS;
}

// Calculate how many updates needs to be done with DEFAULT_UPDATE_DP
// to fill up the time bar
int numberOfUpdates = timeBarWidth / DEFAULT_UPDATE_DP;

if (numberOfUpdates <= 0) {
return MAX_UPDATE_FREQUENCY_MS;
}

// Calculate the designated update interval, taking duration into consideration as well
long mediaTimeUpdatePeriodMs = duration / numberOfUpdates;

if (mediaTimeUpdatePeriodMs <= 0) {
return MAX_UPDATE_FREQUENCY_MS;
}

// Calculate the delay needed from the current position until the next update is due
long mediaTimeDelayMs = mediaTimeUpdatePeriodMs - (position % mediaTimeUpdatePeriodMs);

// If the delay would be too small, then skip the next update
if (mediaTimeDelayMs < (mediaTimeUpdatePeriodMs / 5)) {
mediaTimeDelayMs += mediaTimeUpdatePeriodMs;
}

// Calculate the delay until the next update (in real time), taking
// playbackSpeed into consideration
long delayMs = playbackSpeed == 1 ? mediaTimeDelayMs : (long) (mediaTimeDelayMs / playbackSpeed);

// Limit the delay if needed, to avoid too frequent / infrequent updates
if (delayMs < MIN_UPDATE_FREQUENCY_MS) {
delayMs = MIN_UPDATE_FREQUENCY_MS;
} else if (delayMs >= MAX_UPDATE_FREQUENCY_MS) {
delayMs = MAX_UPDATE_FREQUENCY_MS;
}

return delayMs;
}

private final class ComponentListener
implements Player.EventListener, TimeBar.OnScrubListener, OnClickListener {

Expand Down

0 comments on commit eaf0d40

Please sign in to comment.