diff --git a/demo/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java b/demo/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java index 71e5266ef4..a5e06fa184 100644 --- a/demo/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java +++ b/demo/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java @@ -205,8 +205,6 @@ public class PlayerActivity extends Activity implements OnClickListener, ExoPlay @Override public boolean dispatchKeyEvent(KeyEvent event) { - // Show the controls on any key event. - simpleExoPlayerView.showController(); // If the event was not handled then see if the player view can handle it as a media key event. return super.dispatchKeyEvent(event) || simpleExoPlayerView.dispatchMediaKeyEvent(event); } diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlaybackControlView.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlaybackControlView.java index 2bd576d32e..b06bbf9735 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlaybackControlView.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlaybackControlView.java @@ -959,11 +959,7 @@ public class PlaybackControlView extends FrameLayout { @Override public boolean dispatchKeyEvent(KeyEvent event) { - boolean handled = dispatchMediaKeyEvent(event) || super.dispatchKeyEvent(event); - if (handled) { - show(); - } - return handled; + return dispatchMediaKeyEvent(event) || super.dispatchKeyEvent(event); } /** @@ -1005,7 +1001,6 @@ public class PlaybackControlView extends FrameLayout { break; } } - show(); return true; } diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SimpleExoPlayerView.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SimpleExoPlayerView.java index 5cbfb638a5..3ec9b0943a 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/SimpleExoPlayerView.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/SimpleExoPlayerView.java @@ -88,6 +88,14 @@ import java.util.List; *
The playback controls are automatically hidden during playback after
+ * {{@link #getControllerShowTimeoutMs()}}. They are shown indefinitely when playback has not
+ * started yet, is paused, has ended or failed.
*/
public void showController() {
- if (useController) {
- maybeShowController(true);
- }
+ showController(shouldShowControllerIndefinitely());
}
/**
@@ -550,6 +574,26 @@ public final class SimpleExoPlayerView extends FrameLayout {
this.controllerHideOnTouch = controllerHideOnTouch;
}
+ /**
+ * Returns whether the playback controls are automatically shown when playback starts, pauses,
+ * ends, or fails. If set to false, the playback controls can be manually operated with {@link
+ * #showController()} and {@link #hideController()}.
+ */
+ public boolean getControllerAutoShow() {
+ return controllerAutoShow;
+ }
+
+ /**
+ * Sets whether the playback controls are automatically shown when playback starts, pauses, ends,
+ * or fails. If set to false, the playback controls can be manually operated with {@link
+ * #showController()} and {@link #hideController()}.
+ *
+ * @param controllerAutoShow Whether the playback controls are allowed to show automatically.
+ */
+ public void setControllerAutoShow(boolean controllerAutoShow) {
+ this.controllerAutoShow = controllerAutoShow;
+ }
+
/**
* Set the {@link PlaybackControlView.VisibilityListener}.
*
@@ -664,18 +708,34 @@ public final class SimpleExoPlayerView extends FrameLayout {
return true;
}
+ /**
+ * Shows the playback controls, but only if forced or shown indefinitely.
+ */
private void maybeShowController(boolean isForced) {
- if (!useController || player == null) {
- return;
+ if (useController) {
+ boolean wasShowingIndefinitely = controller.isVisible() && controller.getShowTimeoutMs() <= 0;
+ boolean shouldShowIndefinitely = shouldShowControllerIndefinitely();
+ if (isForced || wasShowingIndefinitely || shouldShowIndefinitely) {
+ showController(shouldShowIndefinitely);
+ }
+ }
+ }
+
+ private boolean shouldShowControllerIndefinitely() {
+ if (player == null) {
+ return true;
}
int playbackState = player.getPlaybackState();
- boolean showIndefinitely = playbackState == ExoPlayer.STATE_IDLE
- || playbackState == ExoPlayer.STATE_ENDED || !player.getPlayWhenReady();
- boolean wasShowingIndefinitely = controller.isVisible() && controller.getShowTimeoutMs() <= 0;
- controller.setShowTimeoutMs(showIndefinitely ? 0 : controllerShowTimeoutMs);
- if (isForced || showIndefinitely || wasShowingIndefinitely) {
- controller.show();
+ return controllerAutoShow && (playbackState == ExoPlayer.STATE_IDLE
+ || playbackState == ExoPlayer.STATE_ENDED || !player.getPlayWhenReady());
+ }
+
+ private void showController(boolean showIndefinitely) {
+ if (!useController) {
+ return;
}
+ controller.setShowTimeoutMs(showIndefinitely ? 0 : controllerShowTimeoutMs);
+ controller.show();
}
private void updateForCurrentTrackSelections() {
diff --git a/library/ui/src/main/res/values/attrs.xml b/library/ui/src/main/res/values/attrs.xml
index 2cb28709b6..ecf3900751 100644
--- a/library/ui/src/main/res/values/attrs.xml
+++ b/library/ui/src/main/res/values/attrs.xml
@@ -45,6 +45,7 @@