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
This commit is contained in:
parent
90b52123ce
commit
76a1dd3da2
@ -748,14 +748,14 @@ public class PlayerControlView extends FrameLayout {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
boolean requestPlayPauseFocus = false;
|
boolean requestPlayPauseFocus = false;
|
||||||
boolean playing = isPlaying();
|
boolean shouldShowPauseButton = shouldShowPauseButton();
|
||||||
if (playButton != null) {
|
if (playButton != null) {
|
||||||
requestPlayPauseFocus |= playing && playButton.isFocused();
|
requestPlayPauseFocus |= shouldShowPauseButton && playButton.isFocused();
|
||||||
playButton.setVisibility(playing ? GONE : VISIBLE);
|
playButton.setVisibility(shouldShowPauseButton ? GONE : VISIBLE);
|
||||||
}
|
}
|
||||||
if (pauseButton != null) {
|
if (pauseButton != null) {
|
||||||
requestPlayPauseFocus |= !playing && pauseButton.isFocused();
|
requestPlayPauseFocus |= !shouldShowPauseButton && pauseButton.isFocused();
|
||||||
pauseButton.setVisibility(!playing ? GONE : VISIBLE);
|
pauseButton.setVisibility(shouldShowPauseButton ? VISIBLE : GONE);
|
||||||
}
|
}
|
||||||
if (requestPlayPauseFocus) {
|
if (requestPlayPauseFocus) {
|
||||||
requestPlayPauseFocus();
|
requestPlayPauseFocus();
|
||||||
@ -943,7 +943,7 @@ public class PlayerControlView extends FrameLayout {
|
|||||||
// Cancel any pending updates and schedule a new one if necessary.
|
// Cancel any pending updates and schedule a new one if necessary.
|
||||||
removeCallbacks(updateProgressAction);
|
removeCallbacks(updateProgressAction);
|
||||||
int playbackState = player == null ? Player.STATE_IDLE : player.getPlaybackState();
|
int playbackState = player == null ? Player.STATE_IDLE : player.getPlaybackState();
|
||||||
if (playbackState == Player.STATE_READY && player.getPlayWhenReady()) {
|
if (player.isPlaying()) {
|
||||||
long mediaTimeDelayMs =
|
long mediaTimeDelayMs =
|
||||||
timeBar != null ? timeBar.getPreferredUpdateDelay() : MAX_UPDATE_INTERVAL_MS;
|
timeBar != null ? timeBar.getPreferredUpdateDelay() : MAX_UPDATE_INTERVAL_MS;
|
||||||
|
|
||||||
@ -965,10 +965,10 @@ public class PlayerControlView extends FrameLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void requestPlayPauseFocus() {
|
private void requestPlayPauseFocus() {
|
||||||
boolean playing = isPlaying();
|
boolean shouldShowPauseButton = shouldShowPauseButton();
|
||||||
if (!playing && playButton != null) {
|
if (!shouldShowPauseButton && playButton != null) {
|
||||||
playButton.requestFocus();
|
playButton.requestFocus();
|
||||||
} else if (playing && pauseButton != null) {
|
} else if (shouldShowPauseButton && pauseButton != null) {
|
||||||
pauseButton.requestFocus();
|
pauseButton.requestFocus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1149,7 +1149,7 @@ public class PlayerControlView extends FrameLayout {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isPlaying() {
|
private boolean shouldShowPauseButton() {
|
||||||
return player != null
|
return player != null
|
||||||
&& player.getPlaybackState() != Player.STATE_ENDED
|
&& player.getPlaybackState() != Player.STATE_ENDED
|
||||||
&& player.getPlaybackState() != Player.STATE_IDLE
|
&& player.getPlaybackState() != Player.STATE_IDLE
|
||||||
@ -1219,6 +1219,11 @@ public class PlayerControlView extends FrameLayout {
|
|||||||
updateProgress();
|
updateProgress();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onIsPlayingChanged(boolean isPlaying) {
|
||||||
|
updateProgress();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRepeatModeChanged(int repeatMode) {
|
public void onRepeatModeChanged(int repeatMode) {
|
||||||
updateRepeatModeButton();
|
updateRepeatModeButton();
|
||||||
|
@ -382,8 +382,6 @@ public class PlayerNotificationManager {
|
|||||||
private int visibility;
|
private int visibility;
|
||||||
@Priority private int priority;
|
@Priority private int priority;
|
||||||
private boolean useChronometer;
|
private boolean useChronometer;
|
||||||
private boolean wasPlayWhenReady;
|
|
||||||
private int lastPlaybackState;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use {@link #createWithNotificationChannel(Context, String, int, int, int,
|
* @deprecated Use {@link #createWithNotificationChannel(Context, String, int, int, int,
|
||||||
@ -663,8 +661,6 @@ public class PlayerNotificationManager {
|
|||||||
}
|
}
|
||||||
this.player = player;
|
this.player = player;
|
||||||
if (player != null) {
|
if (player != null) {
|
||||||
wasPlayWhenReady = player.getPlayWhenReady();
|
|
||||||
lastPlaybackState = player.getPlaybackState();
|
|
||||||
player.addListener(playerListener);
|
player.addListener(playerListener);
|
||||||
startOrUpdateNotification();
|
startOrUpdateNotification();
|
||||||
}
|
}
|
||||||
@ -1070,10 +1066,9 @@ public class PlayerNotificationManager {
|
|||||||
// Changing "showWhen" causes notification flicker if SDK_INT < 21.
|
// Changing "showWhen" causes notification flicker if SDK_INT < 21.
|
||||||
if (Util.SDK_INT >= 21
|
if (Util.SDK_INT >= 21
|
||||||
&& useChronometer
|
&& useChronometer
|
||||||
|
&& player.isPlaying()
|
||||||
&& !player.isPlayingAd()
|
&& !player.isPlayingAd()
|
||||||
&& !player.isCurrentWindowDynamic()
|
&& !player.isCurrentWindowDynamic()) {
|
||||||
&& player.getPlayWhenReady()
|
|
||||||
&& player.getPlaybackState() == Player.STATE_READY) {
|
|
||||||
builder
|
builder
|
||||||
.setWhen(System.currentTimeMillis() - player.getContentPosition())
|
.setWhen(System.currentTimeMillis() - player.getContentPosition())
|
||||||
.setShowWhen(true)
|
.setShowWhen(true)
|
||||||
@ -1138,7 +1133,7 @@ public class PlayerNotificationManager {
|
|||||||
stringActions.add(ACTION_REWIND);
|
stringActions.add(ACTION_REWIND);
|
||||||
}
|
}
|
||||||
if (usePlayPauseActions) {
|
if (usePlayPauseActions) {
|
||||||
if (isPlaying(player)) {
|
if (shouldShowPauseButton(player)) {
|
||||||
stringActions.add(ACTION_PAUSE);
|
stringActions.add(ACTION_PAUSE);
|
||||||
} else {
|
} else {
|
||||||
stringActions.add(ACTION_PLAY);
|
stringActions.add(ACTION_PLAY);
|
||||||
@ -1182,10 +1177,10 @@ public class PlayerNotificationManager {
|
|||||||
if (skipPreviousActionIndex != -1) {
|
if (skipPreviousActionIndex != -1) {
|
||||||
actionIndices[actionCounter++] = skipPreviousActionIndex;
|
actionIndices[actionCounter++] = skipPreviousActionIndex;
|
||||||
}
|
}
|
||||||
boolean isPlaying = isPlaying(player);
|
boolean shouldShowPauseButton = shouldShowPauseButton(player);
|
||||||
if (pauseActionIndex != -1 && isPlaying) {
|
if (pauseActionIndex != -1 && shouldShowPauseButton) {
|
||||||
actionIndices[actionCounter++] = pauseActionIndex;
|
actionIndices[actionCounter++] = pauseActionIndex;
|
||||||
} else if (playActionIndex != -1 && !isPlaying) {
|
} else if (playActionIndex != -1 && !shouldShowPauseButton) {
|
||||||
actionIndices[actionCounter++] = playActionIndex;
|
actionIndices[actionCounter++] = playActionIndex;
|
||||||
}
|
}
|
||||||
if (skipNextActionIndex != -1) {
|
if (skipNextActionIndex != -1) {
|
||||||
@ -1257,7 +1252,7 @@ public class PlayerNotificationManager {
|
|||||||
controlDispatcher.dispatchSeekTo(player, windowIndex, positionMs);
|
controlDispatcher.dispatchSeekTo(player, windowIndex, positionMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isPlaying(Player player) {
|
private boolean shouldShowPauseButton(Player player) {
|
||||||
return player.getPlaybackState() != Player.STATE_ENDED
|
return player.getPlaybackState() != Player.STATE_ENDED
|
||||||
&& player.getPlaybackState() != Player.STATE_IDLE
|
&& player.getPlaybackState() != Player.STATE_IDLE
|
||||||
&& player.getPlayWhenReady();
|
&& player.getPlayWhenReady();
|
||||||
@ -1328,11 +1323,12 @@ public class PlayerNotificationManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
|
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
|
||||||
if (wasPlayWhenReady != playWhenReady || lastPlaybackState != playbackState) {
|
startOrUpdateNotification();
|
||||||
startOrUpdateNotification();
|
}
|
||||||
wasPlayWhenReady = playWhenReady;
|
|
||||||
lastPlaybackState = playbackState;
|
@Override
|
||||||
}
|
public void onIsPlayingChanged(boolean isPlaying) {
|
||||||
|
startOrUpdateNotification();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user