Commit 57840688 by ibaker Committed by Ian Baker

Allow stable API users to use DebugTextViewHelper

This is used from the main demo app.

PiperOrigin-RevId: 445420580
parent 3dff9fbe
...@@ -30,13 +30,13 @@ import java.util.Locale; ...@@ -30,13 +30,13 @@ import java.util.Locale;
* A helper class for periodically updating a {@link TextView} with debug information obtained from * A helper class for periodically updating a {@link TextView} with debug information obtained from
* an {@link ExoPlayer}. * an {@link ExoPlayer}.
*/ */
@UnstableApi public class DebugTextViewHelper {
public class DebugTextViewHelper implements Player.Listener, Runnable {
private static final int REFRESH_INTERVAL_MS = 1000; private static final int REFRESH_INTERVAL_MS = 1000;
private final ExoPlayer player; private final ExoPlayer player;
private final TextView textView; private final TextView textView;
private final Updater updater;
private boolean started; private boolean started;
...@@ -50,6 +50,7 @@ public class DebugTextViewHelper implements Player.Listener, Runnable { ...@@ -50,6 +50,7 @@ public class DebugTextViewHelper implements Player.Listener, Runnable {
Assertions.checkArgument(player.getApplicationLooper() == Looper.getMainLooper()); Assertions.checkArgument(player.getApplicationLooper() == Looper.getMainLooper());
this.player = player; this.player = player;
this.textView = textView; this.textView = textView;
this.updater = new Updater();
} }
/** /**
...@@ -61,7 +62,7 @@ public class DebugTextViewHelper implements Player.Listener, Runnable { ...@@ -61,7 +62,7 @@ public class DebugTextViewHelper implements Player.Listener, Runnable {
return; return;
} }
started = true; started = true;
player.addListener(this); player.addListener(updater);
updateAndPost(); updateAndPost();
} }
...@@ -74,53 +75,28 @@ public class DebugTextViewHelper implements Player.Listener, Runnable { ...@@ -74,53 +75,28 @@ public class DebugTextViewHelper implements Player.Listener, Runnable {
return; return;
} }
started = false; started = false;
player.removeListener(this); player.removeListener(updater);
textView.removeCallbacks(this); textView.removeCallbacks(updater);
}
// Player.Listener implementation.
@Override
public final void onPlaybackStateChanged(@Player.State int playbackState) {
updateAndPost();
}
@Override
public final void onPlayWhenReadyChanged(
boolean playWhenReady, @Player.PlayWhenReadyChangeReason int reason) {
updateAndPost();
}
@Override
public final void onPositionDiscontinuity(
Player.PositionInfo oldPosition,
Player.PositionInfo newPosition,
@Player.DiscontinuityReason int reason) {
updateAndPost();
}
// Runnable implementation.
@Override
public final void run() {
updateAndPost();
} }
// Protected methods. // Protected methods.
@UnstableApi
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
protected final void updateAndPost() { protected final void updateAndPost() {
textView.setText(getDebugString()); textView.setText(getDebugString());
textView.removeCallbacks(this); textView.removeCallbacks(updater);
textView.postDelayed(this, REFRESH_INTERVAL_MS); textView.postDelayed(updater, REFRESH_INTERVAL_MS);
} }
/** Returns the debugging information string to be shown by the target {@link TextView}. */ /** Returns the debugging information string to be shown by the target {@link TextView}. */
@UnstableApi
protected String getDebugString() { protected String getDebugString() {
return getPlayerStateString() + getVideoString() + getAudioString(); return getPlayerStateString() + getVideoString() + getAudioString();
} }
/** Returns a string containing player state debugging information. */ /** Returns a string containing player state debugging information. */
@UnstableApi
protected String getPlayerStateString() { protected String getPlayerStateString() {
String playbackStateString; String playbackStateString;
switch (player.getPlaybackState()) { switch (player.getPlaybackState()) {
...@@ -146,6 +122,7 @@ public class DebugTextViewHelper implements Player.Listener, Runnable { ...@@ -146,6 +122,7 @@ public class DebugTextViewHelper implements Player.Listener, Runnable {
} }
/** Returns a string containing video debugging information. */ /** Returns a string containing video debugging information. */
@UnstableApi
protected String getVideoString() { protected String getVideoString() {
Format format = player.getVideoFormat(); Format format = player.getVideoFormat();
DecoderCounters decoderCounters = player.getVideoDecoderCounters(); DecoderCounters decoderCounters = player.getVideoDecoderCounters();
...@@ -170,6 +147,7 @@ public class DebugTextViewHelper implements Player.Listener, Runnable { ...@@ -170,6 +147,7 @@ public class DebugTextViewHelper implements Player.Listener, Runnable {
} }
/** Returns a string containing audio debugging information. */ /** Returns a string containing audio debugging information. */
@UnstableApi
protected String getAudioString() { protected String getAudioString() {
Format format = player.getAudioFormat(); Format format = player.getAudioFormat();
DecoderCounters decoderCounters = player.getAudioDecoderCounters(); DecoderCounters decoderCounters = player.getAudioDecoderCounters();
...@@ -222,4 +200,35 @@ public class DebugTextViewHelper implements Player.Listener, Runnable { ...@@ -222,4 +200,35 @@ public class DebugTextViewHelper implements Player.Listener, Runnable {
return String.valueOf(averageUs); return String.valueOf(averageUs);
} }
} }
private final class Updater implements Player.Listener, Runnable {
// Player.Listener implementation.
@Override
public void onPlaybackStateChanged(@Player.State int playbackState) {
updateAndPost();
}
@Override
public void onPlayWhenReadyChanged(
boolean playWhenReady, @Player.PlayWhenReadyChangeReason int reason) {
updateAndPost();
}
@Override
public void onPositionDiscontinuity(
Player.PositionInfo oldPosition,
Player.PositionInfo newPosition,
@Player.DiscontinuityReason int reason) {
updateAndPost();
}
// Runnable implementation.
@Override
public void run() {
updateAndPost();
}
}
} }
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