Commit fa4f8766 by tonihei Committed by Oliver Woodman

UI parameter to disable automatically showing playback controls.

(Fixing GitHub issue #2699)

Added controllerAutoShow parameter to decide whether playback controls are
shown automatically. Default is true. Can be overwritten in the XML with
auto_show=false or via SimpleExoPlayerView.setControllerAutoShow(false).

Also inverted the logic of maybeShowControllers and showController.
SimpleExoPlayerView.(show/hide)Controller and PlaybackControlView.(show/hide)
now unconditionally do what they say to allow manual operation.
SimpleExoPlayerView.maybeShowController is used internally to automatically
show playback controls.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=158712277
parent dcc2f9bd
...@@ -205,8 +205,6 @@ public class PlayerActivity extends Activity implements OnClickListener, ExoPlay ...@@ -205,8 +205,6 @@ public class PlayerActivity extends Activity implements OnClickListener, ExoPlay
@Override @Override
public boolean dispatchKeyEvent(KeyEvent event) { 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. // 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); return super.dispatchKeyEvent(event) || simpleExoPlayerView.dispatchMediaKeyEvent(event);
} }
......
...@@ -959,11 +959,7 @@ public class PlaybackControlView extends FrameLayout { ...@@ -959,11 +959,7 @@ public class PlaybackControlView extends FrameLayout {
@Override @Override
public boolean dispatchKeyEvent(KeyEvent event) { public boolean dispatchKeyEvent(KeyEvent event) {
boolean handled = dispatchMediaKeyEvent(event) || super.dispatchKeyEvent(event); return dispatchMediaKeyEvent(event) || super.dispatchKeyEvent(event);
if (handled) {
show();
}
return handled;
} }
/** /**
...@@ -1005,7 +1001,6 @@ public class PlaybackControlView extends FrameLayout { ...@@ -1005,7 +1001,6 @@ public class PlaybackControlView extends FrameLayout {
break; break;
} }
} }
show();
return true; return true;
} }
......
...@@ -88,6 +88,14 @@ import java.util.List; ...@@ -88,6 +88,14 @@ import java.util.List;
* <li>Default: {@code true}</li> * <li>Default: {@code true}</li>
* </ul> * </ul>
* </li> * </li>
* <li><b>{@code auto_show}</b> - 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()}.
* <ul>
* <li>Corresponding method: {@link #setControllerAutoShow(boolean)}</li>
* <li>Default: {@code true}</li>
* </ul>
* </li>
* <li><b>{@code resize_mode}</b> - Controls how video and album art is resized within the view. * <li><b>{@code resize_mode}</b> - Controls how video and album art is resized within the view.
* Valid values are {@code fit}, {@code fixed_width}, {@code fixed_height} and {@code fill}. * Valid values are {@code fit}, {@code fixed_width}, {@code fixed_height} and {@code fill}.
* <ul> * <ul>
...@@ -198,6 +206,7 @@ public final class SimpleExoPlayerView extends FrameLayout { ...@@ -198,6 +206,7 @@ public final class SimpleExoPlayerView extends FrameLayout {
private boolean useArtwork; private boolean useArtwork;
private Bitmap defaultArtwork; private Bitmap defaultArtwork;
private int controllerShowTimeoutMs; private int controllerShowTimeoutMs;
private boolean controllerAutoShow;
private boolean controllerHideOnTouch; private boolean controllerHideOnTouch;
public SimpleExoPlayerView(Context context) { public SimpleExoPlayerView(Context context) {
...@@ -238,6 +247,7 @@ public final class SimpleExoPlayerView extends FrameLayout { ...@@ -238,6 +247,7 @@ public final class SimpleExoPlayerView extends FrameLayout {
int resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT; int resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT;
int controllerShowTimeoutMs = PlaybackControlView.DEFAULT_SHOW_TIMEOUT_MS; int controllerShowTimeoutMs = PlaybackControlView.DEFAULT_SHOW_TIMEOUT_MS;
boolean controllerHideOnTouch = true; boolean controllerHideOnTouch = true;
boolean controllerAutoShow = true;
if (attrs != null) { if (attrs != null) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.SimpleExoPlayerView, 0, 0); R.styleable.SimpleExoPlayerView, 0, 0);
...@@ -254,6 +264,8 @@ public final class SimpleExoPlayerView extends FrameLayout { ...@@ -254,6 +264,8 @@ public final class SimpleExoPlayerView extends FrameLayout {
controllerShowTimeoutMs); controllerShowTimeoutMs);
controllerHideOnTouch = a.getBoolean(R.styleable.SimpleExoPlayerView_hide_on_touch, controllerHideOnTouch = a.getBoolean(R.styleable.SimpleExoPlayerView_hide_on_touch,
controllerHideOnTouch); controllerHideOnTouch);
controllerAutoShow = a.getBoolean(R.styleable.SimpleExoPlayerView_auto_show,
controllerAutoShow);
} finally { } finally {
a.recycle(); a.recycle();
} }
...@@ -317,6 +329,7 @@ public final class SimpleExoPlayerView extends FrameLayout { ...@@ -317,6 +329,7 @@ public final class SimpleExoPlayerView extends FrameLayout {
} }
this.controllerShowTimeoutMs = controller != null ? controllerShowTimeoutMs : 0; this.controllerShowTimeoutMs = controller != null ? controllerShowTimeoutMs : 0;
this.controllerHideOnTouch = controllerHideOnTouch; this.controllerHideOnTouch = controllerHideOnTouch;
this.controllerAutoShow = controllerAutoShow;
this.useController = useController && controller != null; this.useController = useController && controller != null;
hideController(); hideController();
} }
...@@ -480,6 +493,11 @@ public final class SimpleExoPlayerView extends FrameLayout { ...@@ -480,6 +493,11 @@ public final class SimpleExoPlayerView extends FrameLayout {
} }
} }
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
return dispatchMediaKeyEvent(event) || super.dispatchKeyEvent(event);
}
/** /**
* Called to process media key events. Any {@link KeyEvent} can be passed but only media key * Called to process media key events. Any {@link KeyEvent} can be passed but only media key
* events will be handled. Does nothing if playback controls are disabled. * events will be handled. Does nothing if playback controls are disabled.
...@@ -488,16 +506,22 @@ public final class SimpleExoPlayerView extends FrameLayout { ...@@ -488,16 +506,22 @@ public final class SimpleExoPlayerView extends FrameLayout {
* @return Whether the key event was handled. * @return Whether the key event was handled.
*/ */
public boolean dispatchMediaKeyEvent(KeyEvent event) { public boolean dispatchMediaKeyEvent(KeyEvent event) {
return useController && controller.dispatchMediaKeyEvent(event); boolean handled = useController && controller.dispatchMediaKeyEvent(event);
if (handled) {
maybeShowController(true);
}
return handled;
} }
/** /**
* Shows the playback controls. Does nothing if playback controls are disabled. * Shows the playback controls. Does nothing if playback controls are disabled.
*
* <p>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() { public void showController() {
if (useController) { showController(shouldShowControllerIndefinitely());
maybeShowController(true);
}
} }
/** /**
...@@ -551,6 +575,26 @@ public final class SimpleExoPlayerView extends FrameLayout { ...@@ -551,6 +575,26 @@ public final class SimpleExoPlayerView extends FrameLayout {
} }
/** /**
* 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}. * Set the {@link PlaybackControlView.VisibilityListener}.
* *
* @param listener The listener to be notified about visibility changes. * @param listener The listener to be notified about visibility changes.
...@@ -664,19 +708,35 @@ public final class SimpleExoPlayerView extends FrameLayout { ...@@ -664,19 +708,35 @@ public final class SimpleExoPlayerView extends FrameLayout {
return true; return true;
} }
/**
* Shows the playback controls, but only if forced or shown indefinitely.
*/
private void maybeShowController(boolean isForced) { private void maybeShowController(boolean isForced) {
if (!useController || player == null) { if (useController) {
return; 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(); int playbackState = player.getPlaybackState();
boolean showIndefinitely = playbackState == ExoPlayer.STATE_IDLE return controllerAutoShow && (playbackState == ExoPlayer.STATE_IDLE
|| playbackState == ExoPlayer.STATE_ENDED || !player.getPlayWhenReady(); || playbackState == ExoPlayer.STATE_ENDED || !player.getPlayWhenReady());
boolean wasShowingIndefinitely = controller.isVisible() && controller.getShowTimeoutMs() <= 0; }
private void showController(boolean showIndefinitely) {
if (!useController) {
return;
}
controller.setShowTimeoutMs(showIndefinitely ? 0 : controllerShowTimeoutMs); controller.setShowTimeoutMs(showIndefinitely ? 0 : controllerShowTimeoutMs);
if (isForced || showIndefinitely || wasShowingIndefinitely) {
controller.show(); controller.show();
} }
}
private void updateForCurrentTrackSelections() { private void updateForCurrentTrackSelections() {
if (player == null) { if (player == null) {
......
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
<attr name="default_artwork" format="reference"/> <attr name="default_artwork" format="reference"/>
<attr name="use_controller" format="boolean"/> <attr name="use_controller" format="boolean"/>
<attr name="hide_on_touch" format="boolean"/> <attr name="hide_on_touch" format="boolean"/>
<attr name="auto_show" format="boolean"/>
<attr name="resize_mode"/> <attr name="resize_mode"/>
<attr name="surface_type"/> <attr name="surface_type"/>
<attr name="show_timeout"/> <attr name="show_timeout"/>
......
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