Skip to content

Commit

Permalink
Use Player.isPlaying in appropriate places.
Browse files Browse the repository at this point in the history
This method should be used where we previously checked for active playback
by state==READY and playWhenReady=true. Using the new method ensures we take
audio focus into account for these usages.

Also update some method naming to avoid confusion with the isPlaying method.

Issue:#6203
PiperOrigin-RevId: 270910982
  • Loading branch information
tonihei authored and ojw28 committed Oct 2, 2019
1 parent c5b6c62 commit 4df2262
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -750,14 +750,14 @@ private void updatePlayPauseButton() {
return;
}
boolean requestPlayPauseFocus = false;
boolean playing = isPlaying();
boolean shouldShowPauseButton = shouldShowPauseButton();
if (playButton != null) {
requestPlayPauseFocus |= playing && playButton.isFocused();
playButton.setVisibility(playing ? GONE : VISIBLE);
requestPlayPauseFocus |= shouldShowPauseButton && playButton.isFocused();
playButton.setVisibility(shouldShowPauseButton ? GONE : VISIBLE);
}
if (pauseButton != null) {
requestPlayPauseFocus |= !playing && pauseButton.isFocused();
pauseButton.setVisibility(!playing ? GONE : VISIBLE);
requestPlayPauseFocus |= !shouldShowPauseButton && pauseButton.isFocused();
pauseButton.setVisibility(shouldShowPauseButton ? VISIBLE : GONE);
}
if (requestPlayPauseFocus) {
requestPlayPauseFocus();
Expand Down Expand Up @@ -945,7 +945,7 @@ private void updateProgress() {
// Cancel any pending updates and schedule a new one if necessary.
removeCallbacks(updateProgressAction);
int playbackState = player == null ? Player.STATE_IDLE : player.getPlaybackState();
if (playbackState == Player.STATE_READY && player.getPlayWhenReady()) {
if (player.isPlaying()) {
long mediaTimeDelayMs =
timeBar != null ? timeBar.getPreferredUpdateDelay() : MAX_UPDATE_INTERVAL_MS;

Expand All @@ -967,10 +967,10 @@ private void updateProgress() {
}

private void requestPlayPauseFocus() {
boolean playing = isPlaying();
if (!playing && playButton != null) {
boolean shouldShowPauseButton = shouldShowPauseButton();
if (!shouldShowPauseButton && playButton != null) {
playButton.requestFocus();
} else if (playing && pauseButton != null) {
} else if (shouldShowPauseButton && pauseButton != null) {
pauseButton.requestFocus();
}
}
Expand Down Expand Up @@ -1151,7 +1151,7 @@ public boolean dispatchMediaKeyEvent(KeyEvent event) {
return true;
}

private boolean isPlaying() {
private boolean shouldShowPauseButton() {
return player != null
&& player.getPlaybackState() != Player.STATE_ENDED
&& player.getPlaybackState() != Player.STATE_IDLE
Expand Down Expand Up @@ -1221,6 +1221,11 @@ public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playba
updateProgress();
}

@Override
public void onIsPlayingChanged(boolean isPlaying) {
updateProgress();
}

@Override
public void onRepeatModeChanged(int repeatMode) {
updateRepeatModeButton();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,6 @@ public void onBitmap(final Bitmap bitmap) {
private int visibility;
@Priority private int priority;
private boolean useChronometer;
private boolean wasPlayWhenReady;
private int lastPlaybackState;

/**
* @deprecated Use {@link #createWithNotificationChannel(Context, String, int, int, int,
Expand Down Expand Up @@ -663,8 +661,6 @@ public final void setPlayer(@Nullable Player player) {
}
this.player = player;
if (player != null) {
wasPlayWhenReady = player.getPlayWhenReady();
lastPlaybackState = player.getPlaybackState();
player.addListener(playerListener);
startOrUpdateNotification();
}
Expand Down Expand Up @@ -1070,10 +1066,9 @@ protected NotificationCompat.Builder createNotification(
// Changing "showWhen" causes notification flicker if SDK_INT < 21.
if (Util.SDK_INT >= 21
&& useChronometer
&& player.isPlaying()
&& !player.isPlayingAd()
&& !player.isCurrentWindowDynamic()
&& player.getPlayWhenReady()
&& player.getPlaybackState() == Player.STATE_READY) {
&& !player.isCurrentWindowDynamic()) {
builder
.setWhen(System.currentTimeMillis() - player.getContentPosition())
.setShowWhen(true)
Expand Down Expand Up @@ -1138,7 +1133,7 @@ protected List<String> getActions(Player player) {
stringActions.add(ACTION_REWIND);
}
if (usePlayPauseActions) {
if (isPlaying(player)) {
if (shouldShowPauseButton(player)) {
stringActions.add(ACTION_PAUSE);
} else {
stringActions.add(ACTION_PLAY);
Expand Down Expand Up @@ -1182,10 +1177,10 @@ protected int[] getActionIndicesForCompactView(List<String> actionNames, Player
if (skipPreviousActionIndex != -1) {
actionIndices[actionCounter++] = skipPreviousActionIndex;
}
boolean isPlaying = isPlaying(player);
if (pauseActionIndex != -1 && isPlaying) {
boolean shouldShowPauseButton = shouldShowPauseButton(player);
if (pauseActionIndex != -1 && shouldShowPauseButton) {
actionIndices[actionCounter++] = pauseActionIndex;
} else if (playActionIndex != -1 && !isPlaying) {
} else if (playActionIndex != -1 && !shouldShowPauseButton) {
actionIndices[actionCounter++] = playActionIndex;
}
if (skipNextActionIndex != -1) {
Expand Down Expand Up @@ -1257,7 +1252,7 @@ private void seekTo(Player player, int windowIndex, long positionMs) {
controlDispatcher.dispatchSeekTo(player, windowIndex, positionMs);
}

private boolean isPlaying(Player player) {
private boolean shouldShowPauseButton(Player player) {
return player.getPlaybackState() != Player.STATE_ENDED
&& player.getPlaybackState() != Player.STATE_IDLE
&& player.getPlayWhenReady();
Expand Down Expand Up @@ -1328,11 +1323,12 @@ private class PlayerListener implements Player.EventListener {

@Override
public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
if (wasPlayWhenReady != playWhenReady || lastPlaybackState != playbackState) {
startOrUpdateNotification();
wasPlayWhenReady = playWhenReady;
lastPlaybackState = playbackState;
}
startOrUpdateNotification();
}

@Override
public void onIsPlayingChanged(boolean isPlaying) {
startOrUpdateNotification();
}

@Override
Expand Down

0 comments on commit 4df2262

Please sign in to comment.