Commit 0bb32a8f by tonihei Committed by Oliver Woodman

Add IntDef for Player states.

PiperOrigin-RevId: 245925254
parent f64011ae
Showing with 55 additions and 39 deletions
...@@ -255,7 +255,7 @@ import java.util.ArrayList; ...@@ -255,7 +255,7 @@ import java.util.ArrayList;
// Player.EventListener implementation. // Player.EventListener implementation.
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
updateCurrentItemIndex(); updateCurrentItemIndex();
} }
......
...@@ -627,7 +627,7 @@ public class PlayerActivity extends AppCompatActivity ...@@ -627,7 +627,7 @@ public class PlayerActivity extends AppCompatActivity
private class PlayerEventListener implements Player.EventListener { private class PlayerEventListener implements Player.EventListener {
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
if (playbackState == Player.STATE_ENDED) { if (playbackState == Player.STATE_ENDED) {
showControls(); showControls();
} }
......
...@@ -94,7 +94,7 @@ public final class CastPlayer extends BasePlayer { ...@@ -94,7 +94,7 @@ public final class CastPlayer extends BasePlayer {
private CastTimeline currentTimeline; private CastTimeline currentTimeline;
private TrackGroupArray currentTrackGroups; private TrackGroupArray currentTrackGroups;
private TrackSelectionArray currentTrackSelection; private TrackSelectionArray currentTrackSelection;
private int playbackState; @Player.State private int playbackState;
private int repeatMode; private int repeatMode;
private int currentWindowIndex; private int currentWindowIndex;
private boolean playWhenReady; private boolean playWhenReady;
...@@ -305,6 +305,7 @@ public final class CastPlayer extends BasePlayer { ...@@ -305,6 +305,7 @@ public final class CastPlayer extends BasePlayer {
} }
@Override @Override
@Player.State
public int getPlaybackState() { public int getPlaybackState() {
return playbackState; return playbackState;
} }
......
...@@ -101,7 +101,7 @@ public class FlacPlaybackTest { ...@@ -101,7 +101,7 @@ public class FlacPlaybackTest {
} }
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
if (playbackState == Player.STATE_ENDED if (playbackState == Player.STATE_ENDED
|| (playbackState == Player.STATE_IDLE && playbackException != null)) { || (playbackState == Player.STATE_IDLE && playbackException != null)) {
player.release(); player.release();
......
...@@ -963,7 +963,7 @@ public final class ImaAdsLoader ...@@ -963,7 +963,7 @@ public final class ImaAdsLoader
} }
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
if (adsManager == null) { if (adsManager == null) {
return; return;
} }
......
...@@ -31,7 +31,7 @@ import java.util.ArrayList; ...@@ -31,7 +31,7 @@ import java.util.ArrayList;
private final Timeline timeline; private final Timeline timeline;
private boolean prepared; private boolean prepared;
private int state; @Player.State private int state;
private boolean playWhenReady; private boolean playWhenReady;
private long position; private long position;
private long contentPosition; private long contentPosition;
...@@ -96,8 +96,8 @@ import java.util.ArrayList; ...@@ -96,8 +96,8 @@ import java.util.ArrayList;
} }
} }
/** Sets the state of this player with the given {@code STATE} constant. */ /** Sets the {@link Player.State} of this player. */
public void setState(int state, boolean playWhenReady) { public void setState(@Player.State int state, boolean playWhenReady) {
boolean notify = this.state != state || this.playWhenReady != playWhenReady; boolean notify = this.state != state || this.playWhenReady != playWhenReady;
this.state = state; this.state = state;
this.playWhenReady = playWhenReady; this.playWhenReady = playWhenReady;
...@@ -131,6 +131,7 @@ import java.util.ArrayList; ...@@ -131,6 +131,7 @@ import java.util.ArrayList;
} }
@Override @Override
@Player.State
public int getPlaybackState() { public int getPlaybackState() {
return state; return state;
} }
......
...@@ -18,13 +18,13 @@ package com.google.android.exoplayer2.ext.leanback; ...@@ -18,13 +18,13 @@ package com.google.android.exoplayer2.ext.leanback;
import android.content.Context; import android.content.Context;
import android.os.Handler; import android.os.Handler;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import android.util.Pair;
import android.view.Surface;
import android.view.SurfaceHolder;
import androidx.leanback.R; import androidx.leanback.R;
import androidx.leanback.media.PlaybackGlueHost; import androidx.leanback.media.PlaybackGlueHost;
import androidx.leanback.media.PlayerAdapter; import androidx.leanback.media.PlayerAdapter;
import androidx.leanback.media.SurfaceHolderGlueHost; import androidx.leanback.media.SurfaceHolderGlueHost;
import android.util.Pair;
import android.view.Surface;
import android.view.SurfaceHolder;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ControlDispatcher; import com.google.android.exoplayer2.ControlDispatcher;
import com.google.android.exoplayer2.DefaultControlDispatcher; import com.google.android.exoplayer2.DefaultControlDispatcher;
...@@ -271,7 +271,7 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter implements Runnab ...@@ -271,7 +271,7 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter implements Runnab
// Player.EventListener implementation. // Player.EventListener implementation.
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
notifyStateChanged(); notifyStateChanged();
} }
......
...@@ -994,7 +994,7 @@ public final class MediaSessionConnector { ...@@ -994,7 +994,7 @@ public final class MediaSessionConnector {
} }
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
invalidateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
} }
......
...@@ -101,7 +101,7 @@ public class OpusPlaybackTest { ...@@ -101,7 +101,7 @@ public class OpusPlaybackTest {
} }
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
if (playbackState == Player.STATE_ENDED if (playbackState == Player.STATE_ENDED
|| (playbackState == Player.STATE_IDLE && playbackException != null)) { || (playbackState == Player.STATE_IDLE && playbackException != null)) {
player.release(); player.release();
......
...@@ -139,7 +139,7 @@ public class VpxPlaybackTest { ...@@ -139,7 +139,7 @@ public class VpxPlaybackTest {
} }
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
if (playbackState == Player.STATE_ENDED if (playbackState == Player.STATE_ENDED
|| (playbackState == Player.STATE_IDLE && playbackException != null)) { || (playbackState == Player.STATE_IDLE && playbackException != null)) {
player.release(); player.release();
......
...@@ -193,6 +193,7 @@ import java.util.concurrent.CopyOnWriteArrayList; ...@@ -193,6 +193,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
} }
@Override @Override
@Player.State
public int getPlaybackState() { public int getPlaybackState() {
return playbackInfo.playbackState; return playbackInfo.playbackState;
} }
...@@ -658,7 +659,7 @@ import java.util.concurrent.CopyOnWriteArrayList; ...@@ -658,7 +659,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
} }
private PlaybackInfo getResetPlaybackInfo( private PlaybackInfo getResetPlaybackInfo(
boolean resetPosition, boolean resetState, int playbackState) { boolean resetPosition, boolean resetState, @Player.State int playbackState) {
if (resetPosition) { if (resetPosition) {
maskingWindowIndex = 0; maskingWindowIndex = 0;
maskingPeriodIndex = 0; maskingPeriodIndex = 0;
......
...@@ -52,7 +52,7 @@ import com.google.android.exoplayer2.trackselection.TrackSelectorResult; ...@@ -52,7 +52,7 @@ import com.google.android.exoplayer2.trackselection.TrackSelectorResult;
*/ */
public final long contentPositionUs; public final long contentPositionUs;
/** The current playback state. One of the {@link Player}.STATE_ constants. */ /** The current playback state. One of the {@link Player}.STATE_ constants. */
public final int playbackState; @Player.State public final int playbackState;
/** Whether the player is currently loading. */ /** Whether the player is currently loading. */
public final boolean isLoading; public final boolean isLoading;
/** The currently available track groups. */ /** The currently available track groups. */
...@@ -128,7 +128,7 @@ import com.google.android.exoplayer2.trackselection.TrackSelectorResult; ...@@ -128,7 +128,7 @@ import com.google.android.exoplayer2.trackselection.TrackSelectorResult;
MediaPeriodId periodId, MediaPeriodId periodId,
long startPositionUs, long startPositionUs,
long contentPositionUs, long contentPositionUs,
int playbackState, @Player.State int playbackState,
boolean isLoading, boolean isLoading,
TrackGroupArray trackGroups, TrackGroupArray trackGroups,
TrackSelectorResult trackSelectorResult, TrackSelectorResult trackSelectorResult,
......
...@@ -361,9 +361,9 @@ public interface Player { ...@@ -361,9 +361,9 @@ public interface Player {
* #getPlaybackState()} changes. * #getPlaybackState()} changes.
* *
* @param playWhenReady Whether playback will proceed when ready. * @param playWhenReady Whether playback will proceed when ready.
* @param playbackState One of the {@code STATE} constants. * @param playbackState The new {@link State playback state}.
*/ */
default void onPlayerStateChanged(boolean playWhenReady, int playbackState) {} default void onPlayerStateChanged(boolean playWhenReady, @State int playbackState) {}
/** /**
* Called when the value of {@link #getRepeatMode()} changes. * Called when the value of {@link #getRepeatMode()} changes.
...@@ -444,6 +444,14 @@ public interface Player { ...@@ -444,6 +444,14 @@ public interface Player {
} }
/** /**
* Playback state. One of {@link #STATE_IDLE}, {@link #STATE_BUFFERING}, {@link #STATE_READY} or
* {@link #STATE_ENDED}.
*/
@Documented
@Retention(RetentionPolicy.SOURCE)
@IntDef({STATE_IDLE, STATE_BUFFERING, STATE_READY, STATE_ENDED})
@interface State {}
/**
* The player does not have any media to play. * The player does not have any media to play.
*/ */
int STATE_IDLE = 1; int STATE_IDLE = 1;
...@@ -581,10 +589,11 @@ public interface Player { ...@@ -581,10 +589,11 @@ public interface Player {
void removeListener(EventListener listener); void removeListener(EventListener listener);
/** /**
* Returns the current state of the player. * Returns the current {@link State playback state} of the player.
* *
* @return One of the {@code STATE} constants defined in this interface. * @return The current {@link State playback state}.
*/ */
@State
int getPlaybackState(); int getPlaybackState();
/** /**
......
...@@ -873,6 +873,7 @@ public class SimpleExoPlayer extends BasePlayer ...@@ -873,6 +873,7 @@ public class SimpleExoPlayer extends BasePlayer
} }
@Override @Override
@Player.State
public int getPlaybackState() { public int getPlaybackState() {
verifyApplicationThread(); verifyApplicationThread();
return player.getPlaybackState(); return player.getPlaybackState();
......
...@@ -464,7 +464,7 @@ public class AnalyticsCollector ...@@ -464,7 +464,7 @@ public class AnalyticsCollector
} }
@Override @Override
public final void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public final void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
EventTime eventTime = generatePlayingMediaPeriodEventTime(); EventTime eventTime = generatePlayingMediaPeriodEventTime();
for (AnalyticsListener listener : listeners) { for (AnalyticsListener listener : listeners) {
listener.onPlayerStateChanged(eventTime, playWhenReady, playbackState); listener.onPlayerStateChanged(eventTime, playWhenReady, playbackState);
......
...@@ -127,10 +127,10 @@ public interface AnalyticsListener { ...@@ -127,10 +127,10 @@ public interface AnalyticsListener {
* *
* @param eventTime The event time. * @param eventTime The event time.
* @param playWhenReady Whether the playback will proceed when ready. * @param playWhenReady Whether the playback will proceed when ready.
* @param playbackState One of the {@link Player}.STATE constants. * @param playbackState The new {@link Player.State playback state}.
*/ */
default void onPlayerStateChanged( default void onPlayerStateChanged(
EventTime eventTime, boolean playWhenReady, int playbackState) {} EventTime eventTime, boolean playWhenReady, @Player.State int playbackState) {}
/** /**
* Called when the timeline changed. * Called when the timeline changed.
......
...@@ -93,7 +93,8 @@ public class EventLogger implements AnalyticsListener { ...@@ -93,7 +93,8 @@ public class EventLogger implements AnalyticsListener {
} }
@Override @Override
public void onPlayerStateChanged(EventTime eventTime, boolean playWhenReady, int state) { public void onPlayerStateChanged(
EventTime eventTime, boolean playWhenReady, @Player.State int state) {
logd(eventTime, "state", playWhenReady + ", " + getStateString(state)); logd(eventTime, "state", playWhenReady + ", " + getStateString(state));
} }
......
...@@ -509,7 +509,7 @@ public final class ExoPlayerTest { ...@@ -509,7 +509,7 @@ public final class ExoPlayerTest {
private int currentPlaybackState = Player.STATE_IDLE; private int currentPlaybackState = Player.STATE_IDLE;
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
currentPlaybackState = playbackState; currentPlaybackState = playbackState;
} }
...@@ -2113,7 +2113,7 @@ public final class ExoPlayerTest { ...@@ -2113,7 +2113,7 @@ public final class ExoPlayerTest {
final EventListener eventListener1 = final EventListener eventListener1 =
new EventListener() { new EventListener() {
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
eventListener1States.add(playbackState); eventListener1States.add(playbackState);
if (playbackState == Player.STATE_READY) { if (playbackState == Player.STATE_READY) {
playerReference.get().stop(/* reset= */ true); playerReference.get().stop(/* reset= */ true);
...@@ -2123,7 +2123,7 @@ public final class ExoPlayerTest { ...@@ -2123,7 +2123,7 @@ public final class ExoPlayerTest {
final EventListener eventListener2 = final EventListener eventListener2 =
new EventListener() { new EventListener() {
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
eventListener2States.add(playbackState); eventListener2States.add(playbackState);
} }
}; };
...@@ -2170,7 +2170,7 @@ public final class ExoPlayerTest { ...@@ -2170,7 +2170,7 @@ public final class ExoPlayerTest {
} }
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
eventListenerPlayWhenReady.add(playWhenReady); eventListenerPlayWhenReady.add(playWhenReady);
eventListenerStates.add(playbackState); eventListenerStates.add(playbackState);
if (playbackState == Player.STATE_READY) { if (playbackState == Player.STATE_READY) {
...@@ -2219,7 +2219,7 @@ public final class ExoPlayerTest { ...@@ -2219,7 +2219,7 @@ public final class ExoPlayerTest {
EventListener eventListener = EventListener eventListener =
new EventListener() { new EventListener() {
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
if (playbackState == Player.STATE_READY && clockAtStartMs.get() == C.TIME_UNSET) { if (playbackState == Player.STATE_READY && clockAtStartMs.get() == C.TIME_UNSET) {
clockAtStartMs.set(clock.elapsedRealtime()); clockAtStartMs.set(clock.elapsedRealtime());
} }
......
...@@ -913,7 +913,7 @@ public final class AnalyticsCollectorTest { ...@@ -913,7 +913,7 @@ public final class AnalyticsCollectorTest {
@Override @Override
public void onPlayerStateChanged( public void onPlayerStateChanged(
EventTime eventTime, boolean playWhenReady, int playbackState) { EventTime eventTime, boolean playWhenReady, @Player.State int playbackState) {
reportedEvents.add(new ReportedEvent(EVENT_PLAYER_STATE_CHANGED, eventTime)); reportedEvents.add(new ReportedEvent(EVENT_PLAYER_STATE_CHANGED, eventTime));
} }
......
...@@ -79,7 +79,7 @@ public class DebugTextViewHelper implements Player.EventListener, Runnable { ...@@ -79,7 +79,7 @@ public class DebugTextViewHelper implements Player.EventListener, Runnable {
// Player.EventListener implementation. // Player.EventListener implementation.
@Override @Override
public final void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public final void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
updateAndPost(); updateAndPost();
} }
......
...@@ -1130,7 +1130,7 @@ public class PlayerControlView extends FrameLayout { ...@@ -1130,7 +1130,7 @@ public class PlayerControlView extends FrameLayout {
} }
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
updatePlayPauseButton(); updatePlayPauseButton();
updateProgress(); updateProgress();
} }
......
...@@ -1276,7 +1276,7 @@ public class PlayerNotificationManager { ...@@ -1276,7 +1276,7 @@ public class PlayerNotificationManager {
private class PlayerListener implements Player.EventListener { private class PlayerListener implements Player.EventListener {
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
if (wasPlayWhenReady != playWhenReady || lastPlaybackState != playbackState) { if (wasPlayWhenReady != playWhenReady || lastPlaybackState != playbackState) {
startOrUpdateNotification(); startOrUpdateNotification();
wasPlayWhenReady = playWhenReady; wasPlayWhenReady = playWhenReady;
......
...@@ -1427,7 +1427,7 @@ public class PlayerView extends FrameLayout implements AdsLoader.AdViewProvider ...@@ -1427,7 +1427,7 @@ public class PlayerView extends FrameLayout implements AdsLoader.AdViewProvider
// Player.EventListener implementation // Player.EventListener implementation
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
updateBuffering(); updateBuffering();
updateErrorMessage(); updateErrorMessage();
if (isPlayingAd() && controllerHideDuringAds) { if (isPlayingAd() && controllerHideDuringAds) {
......
...@@ -669,7 +669,8 @@ public abstract class Action { ...@@ -669,7 +669,8 @@ public abstract class Action {
player.addListener( player.addListener(
new Player.EventListener() { new Player.EventListener() {
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(
boolean playWhenReady, @Player.State int playbackState) {
if (targetPlaybackState == playbackState) { if (targetPlaybackState == playbackState) {
player.removeListener(this); player.removeListener(this);
nextAction.schedule(player, trackSelector, surface, handler); nextAction.schedule(player, trackSelector, surface, handler);
......
...@@ -183,7 +183,7 @@ public abstract class ExoHostedTest implements AnalyticsListener, HostedTest { ...@@ -183,7 +183,7 @@ public abstract class ExoHostedTest implements AnalyticsListener, HostedTest {
@Override @Override
public final void onPlayerStateChanged( public final void onPlayerStateChanged(
EventTime eventTime, boolean playWhenReady, int playbackState) { EventTime eventTime, boolean playWhenReady, @Player.State int playbackState) {
Log.d(tag, "state [" + playWhenReady + ", " + playbackState + "]"); Log.d(tag, "state [" + playWhenReady + ", " + playbackState + "]");
playerWasPrepared |= playbackState != Player.STATE_IDLE; playerWasPrepared |= playbackState != Player.STATE_IDLE;
if (playbackState == Player.STATE_ENDED if (playbackState == Player.STATE_ENDED
......
...@@ -589,7 +589,7 @@ public final class ExoPlayerTestRunner implements Player.EventListener, ActionSc ...@@ -589,7 +589,7 @@ public final class ExoPlayerTestRunner implements Player.EventListener, ActionSc
} }
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlayerStateChanged(boolean playWhenReady, @Player.State int playbackState) {
playerWasPrepared |= playbackState != Player.STATE_IDLE; playerWasPrepared |= playbackState != Player.STATE_IDLE;
if (playbackState == Player.STATE_ENDED if (playbackState == Player.STATE_ENDED
|| (playbackState == Player.STATE_IDLE && playerWasPrepared)) { || (playbackState == Player.STATE_IDLE && playerWasPrepared)) {
......
...@@ -75,6 +75,7 @@ public abstract class StubExoPlayer extends BasePlayer implements ExoPlayer { ...@@ -75,6 +75,7 @@ public abstract class StubExoPlayer extends BasePlayer implements ExoPlayer {
} }
@Override @Override
@Player.State
public int getPlaybackState() { public int getPlaybackState() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment