From 76a1dd3da2039c2f4ac46e0b0993a4df163e4b83 Mon Sep 17 00:00:00 2001 From: tonihei Date: Tue, 24 Sep 2019 16:35:36 +0100 Subject: [PATCH] Use Player.isPlaying in appropriate places. 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 --- .../exoplayer2/ui/PlayerControlView.java | 25 +++++++++------- .../ui/PlayerNotificationManager.java | 30 ++++++++----------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerControlView.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerControlView.java index 358dd14576..7e206369d4 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerControlView.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerControlView.java @@ -748,14 +748,14 @@ public class PlayerControlView extends FrameLayout { 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(); @@ -943,7 +943,7 @@ public class PlayerControlView extends FrameLayout { // 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; @@ -965,10 +965,10 @@ public class PlayerControlView extends FrameLayout { } 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(); } } @@ -1149,7 +1149,7 @@ public class PlayerControlView extends FrameLayout { return true; } - private boolean isPlaying() { + private boolean shouldShowPauseButton() { return player != null && player.getPlaybackState() != Player.STATE_ENDED && player.getPlaybackState() != Player.STATE_IDLE @@ -1219,6 +1219,11 @@ public class PlayerControlView extends FrameLayout { updateProgress(); } + @Override + public void onIsPlayingChanged(boolean isPlaying) { + updateProgress(); + } + @Override public void onRepeatModeChanged(int repeatMode) { updateRepeatModeButton(); diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java index 7fa4b60314..12d4f15922 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java @@ -382,8 +382,6 @@ public class PlayerNotificationManager { 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, @@ -663,8 +661,6 @@ public class PlayerNotificationManager { } this.player = player; if (player != null) { - wasPlayWhenReady = player.getPlayWhenReady(); - lastPlaybackState = player.getPlaybackState(); player.addListener(playerListener); startOrUpdateNotification(); } @@ -1070,10 +1066,9 @@ public class PlayerNotificationManager { // 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) @@ -1138,7 +1133,7 @@ public class PlayerNotificationManager { stringActions.add(ACTION_REWIND); } if (usePlayPauseActions) { - if (isPlaying(player)) { + if (shouldShowPauseButton(player)) { stringActions.add(ACTION_PAUSE); } else { stringActions.add(ACTION_PLAY); @@ -1182,10 +1177,10 @@ public class PlayerNotificationManager { 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) { @@ -1257,7 +1252,7 @@ public class PlayerNotificationManager { 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(); @@ -1328,11 +1323,12 @@ public class PlayerNotificationManager { @Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { - if (wasPlayWhenReady != playWhenReady || lastPlaybackState != playbackState) { - startOrUpdateNotification(); - wasPlayWhenReady = playWhenReady; - lastPlaybackState = playbackState; - } + startOrUpdateNotification(); + } + + @Override + public void onIsPlayingChanged(boolean isPlaying) { + startOrUpdateNotification(); } @Override