Skip to content

Commit

Permalink
Add an option to turn off hiding controls during ads
Browse files Browse the repository at this point in the history
Issue: google#3532

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178372763
  • Loading branch information
andrewlewis authored and ojw28 committed Dec 12, 2017
1 parent 88d012b commit 3e9c86f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 55 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* Skip ads before the ad preceding the player's initial seek position
([#3527](https://github.com/google/ExoPlayer/issues/3527)).
* Fix ad loading when there is no preroll.
* Add an option to turn off hiding controls during ad playback
([#3532](https://github.com/google/ExoPlayer/issues/3532)).

### 2.6.0 ###

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,146 +56,144 @@
/**
* A high level view for {@link SimpleExoPlayer} media playbacks. It displays video, subtitles and
* album art during playback, and displays playback controls using a {@link PlaybackControlView}.
* <p>
* A SimpleExoPlayerView can be customized by setting attributes (or calling corresponding methods),
* overriding the view's layout file or by specifying a custom view layout file, as outlined below.
*
* <p>A SimpleExoPlayerView can be customized by setting attributes (or calling corresponding
* methods), overriding the view's layout file or by specifying a custom view layout file, as
* outlined below.
*
* <h3>Attributes</h3>
*
* The following attributes can be set on a SimpleExoPlayerView when used in a layout XML file:
*
* <p>
*
* <ul>
* <li><b>{@code use_artwork}</b> - Whether artwork is used if available in audio streams.
* <ul>
* <li>Corresponding method: {@link #setUseArtwork(boolean)}</li>
* <li>Default: {@code true}</li>
* <li>Corresponding method: {@link #setUseArtwork(boolean)}
* <li>Default: {@code true}
* </ul>
* </li>
* <li><b>{@code default_artwork}</b> - Default artwork to use if no artwork available in audio
* streams.
* <ul>
* <li>Corresponding method: {@link #setDefaultArtwork(Bitmap)}</li>
* <li>Default: {@code null}</li>
* <li>Corresponding method: {@link #setDefaultArtwork(Bitmap)}
* <li>Default: {@code null}
* </ul>
* </li>
* <li><b>{@code use_controller}</b> - Whether the playback controls can be shown.
* <ul>
* <li>Corresponding method: {@link #setUseController(boolean)}</li>
* <li>Default: {@code true}</li>
* <li>Corresponding method: {@link #setUseController(boolean)}
* <li>Default: {@code true}
* </ul>
* </li>
* <li><b>{@code hide_on_touch}</b> - Whether the playback controls are hidden by touch events.
* <ul>
* <li>Corresponding method: {@link #setControllerHideOnTouch(boolean)}</li>
* <li>Default: {@code true}</li>
* <li>Corresponding method: {@link #setControllerHideOnTouch(boolean)}
* <li>Default: {@code true}
* </ul>
* </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>
* <li>Corresponding method: {@link #setControllerAutoShow(boolean)}
* <li>Default: {@code true}
* </ul>
* <li><b>{@code hide_during_ads}</b> - Whether the playback controls are hidden during ads.
* Controls are always shown during ads if they are enabled and the player is paused.
* <ul>
* <li>Corresponding method: {@link #setControllerHideDuringAds(boolean)}
* <li>Default: {@code true}
* </ul>
* </li>
* <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}.
* <ul>
* <li>Corresponding method: {@link #setResizeMode(int)}</li>
* <li>Default: {@code fit}</li>
* <li>Corresponding method: {@link #setResizeMode(int)}
* <li>Default: {@code fit}
* </ul>
* </li>
* <li><b>{@code surface_type}</b> - The type of surface view used for video playbacks. Valid
* values are {@code surface_view}, {@code texture_view} and {@code none}. Using {@code none}
* is recommended for audio only applications, since creating the surface can be expensive.
* Using {@code surface_view} is recommended for video applications.
* <ul>
* <li>Corresponding method: None</li>
* <li>Default: {@code surface_view}</li>
* <li>Corresponding method: None
* <li>Default: {@code surface_view}
* </ul>
* </li>
* <li><b>{@code shutter_background_color}</b> - The background color of the {@code exo_shutter}
* view.
* <ul>
* <li>Corresponding method: {@link #setShutterBackgroundColor(int)}</li>
* <li>Default: {@code unset}</li>
* <li>Corresponding method: {@link #setShutterBackgroundColor(int)}
* <li>Default: {@code unset}
* </ul>
* </li>
* <li><b>{@code player_layout_id}</b> - Specifies the id of the layout to be inflated. See below
* for more details.
* <ul>
* <li>Corresponding method: None</li>
* <li>Default: {@code R.id.exo_simple_player_view}</li>
* <li>Corresponding method: None
* <li>Default: {@code R.id.exo_simple_player_view}
* </ul>
* <li><b>{@code controller_layout_id}</b> - Specifies the id of the layout resource to be
* inflated by the child {@link PlaybackControlView}. See below for more details.
* <ul>
* <li>Corresponding method: None</li>
* <li>Default: {@code R.id.exo_playback_control_view}</li>
* <li>Corresponding method: None
* <li>Default: {@code R.id.exo_playback_control_view}
* </ul>
* <li>All attributes that can be set on a {@link PlaybackControlView} can also be set on a
* SimpleExoPlayerView, and will be propagated to the inflated {@link PlaybackControlView}
* unless the layout is overridden to specify a custom {@code exo_controller} (see below).
* </li>
* </ul>
*
* <h3>Overriding the layout file</h3>
*
* To customize the layout of SimpleExoPlayerView throughout your app, or just for certain
* configurations, you can define {@code exo_simple_player_view.xml} layout files in your
* application {@code res/layout*} directories. These layouts will override the one provided by the
* ExoPlayer library, and will be inflated for use by SimpleExoPlayerView. The view identifies and
* binds its children by looking for the following ids:
*
* <p>
*
* <ul>
* <li><b>{@code exo_content_frame}</b> - A frame whose aspect ratio is resized based on the video
* or album art of the media being played, and the configured {@code resize_mode}. The video
* surface view is inflated into this frame as its first child.
* <ul>
* <li>Type: {@link AspectRatioFrameLayout}</li>
* <li>Type: {@link AspectRatioFrameLayout}
* </ul>
* </li>
* <li><b>{@code exo_shutter}</b> - A view that's made visible when video should be hidden. This
* view is typically an opaque view that covers the video surface view, thereby obscuring it
* when visible.
* <ul>
* <li>Type: {@link View}</li>
* <li>Type: {@link View}
* </ul>
* </li>
* <li><b>{@code exo_subtitles}</b> - Displays subtitles.
* <ul>
* <li>Type: {@link SubtitleView}</li>
* <li>Type: {@link SubtitleView}
* </ul>
* </li>
* <li><b>{@code exo_artwork}</b> - Displays album art.
* <ul>
* <li>Type: {@link ImageView}</li>
* <li>Type: {@link ImageView}
* </ul>
* </li>
* <li><b>{@code exo_controller_placeholder}</b> - A placeholder that's replaced with the inflated
* {@link PlaybackControlView}. Ignored if an {@code exo_controller} view exists.
* <ul>
* <li>Type: {@link View}</li>
* <li>Type: {@link View}
* </ul>
* </li>
* <li><b>{@code exo_controller}</b> - An already inflated {@link PlaybackControlView}. Allows use
* of a custom extension of {@link PlaybackControlView}. Note that attributes such as
* {@code rewind_increment} will not be automatically propagated through to this instance. If
* a view exists with this id, any {@code exo_controller_placeholder} view will be ignored.
* of a custom extension of {@link PlaybackControlView}. Note that attributes such as {@code
* rewind_increment} will not be automatically propagated through to this instance. If a view
* exists with this id, any {@code exo_controller_placeholder} view will be ignored.
* <ul>
* <li>Type: {@link PlaybackControlView}</li>
* <li>Type: {@link PlaybackControlView}
* </ul>
* </li>
* <li><b>{@code exo_overlay}</b> - A {@link FrameLayout} positioned on top of the player which
* the app can access via {@link #getOverlayFrameLayout()}, provided for convenience.
* <ul>
* <li>Type: {@link FrameLayout}</li>
* <li>Type: {@link FrameLayout}
* </ul>
* </li>
* </ul>
* <p>
* All child views are optional and so can be omitted if not required, however where defined they
*
* <p>All child views are optional and so can be omitted if not required, however where defined they
* must be of the expected type.
*
* <h3>Specifying a custom layout file</h3>
*
* Defining your own {@code exo_simple_player_view.xml} is useful to customize the layout of
* SimpleExoPlayerView throughout your application. It's also possible to customize the layout for a
* single instance in a layout file. This is achieved by setting the {@code player_layout_id}
Expand Down Expand Up @@ -224,6 +222,7 @@ public final class SimpleExoPlayerView extends FrameLayout {
private Bitmap defaultArtwork;
private int controllerShowTimeoutMs;
private boolean controllerAutoShow;
private boolean controllerHideDuringAds;
private boolean controllerHideOnTouch;

public SimpleExoPlayerView(Context context) {
Expand Down Expand Up @@ -267,6 +266,7 @@ public SimpleExoPlayerView(Context context, AttributeSet attrs, int defStyleAttr
int controllerShowTimeoutMs = PlaybackControlView.DEFAULT_SHOW_TIMEOUT_MS;
boolean controllerHideOnTouch = true;
boolean controllerAutoShow = true;
boolean controllerHideDuringAds = true;
if (attrs != null) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.SimpleExoPlayerView, 0, 0);
Expand All @@ -288,6 +288,8 @@ public SimpleExoPlayerView(Context context, AttributeSet attrs, int defStyleAttr
controllerHideOnTouch);
controllerAutoShow = a.getBoolean(R.styleable.SimpleExoPlayerView_auto_show,
controllerAutoShow);
controllerHideDuringAds =
a.getBoolean(R.styleable.SimpleExoPlayerView_hide_during_ads, controllerHideDuringAds);
} finally {
a.recycle();
}
Expand Down Expand Up @@ -358,6 +360,7 @@ public SimpleExoPlayerView(Context context, AttributeSet attrs, int defStyleAttr
this.controllerShowTimeoutMs = controller != null ? controllerShowTimeoutMs : 0;
this.controllerHideOnTouch = controllerHideOnTouch;
this.controllerAutoShow = controllerAutoShow;
this.controllerHideDuringAds = controllerHideDuringAds;
this.useController = useController && controller != null;
hideController();
}
Expand Down Expand Up @@ -649,6 +652,16 @@ public void setControllerAutoShow(boolean controllerAutoShow) {
this.controllerAutoShow = controllerAutoShow;
}

/**
* Sets whether the playback controls are hidden when ads are playing. Controls are always shown
* during ads if they are enabled and the player is paused.
*
* @param controllerHideDuringAds Whether the playback controls are hidden when ads are playing.
*/
public void setControllerHideDuringAds(boolean controllerHideDuringAds) {
this.controllerHideDuringAds = controllerHideDuringAds;
}

/**
* Set the {@link PlaybackControlView.VisibilityListener}.
*
Expand Down Expand Up @@ -784,8 +797,7 @@ public boolean onTrackballEvent(MotionEvent ev) {
* Shows the playback controls, but only if forced or shown indefinitely.
*/
private void maybeShowController(boolean isForced) {
if (isPlayingAd()) {
// Never show the controller if an ad is currently playing.
if (isPlayingAd() && controllerHideDuringAds) {
return;
}
if (useController) {
Expand Down Expand Up @@ -956,7 +968,7 @@ public void onTracksChanged(TrackGroupArray tracks, TrackSelectionArray selectio

@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (isPlayingAd()) {
if (isPlayingAd() && controllerHideDuringAds) {
hideController();
} else {
maybeShowController(false);
Expand All @@ -965,7 +977,7 @@ public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

@Override
public void onPositionDiscontinuity(@DiscontinuityReason int reason) {
if (isPlayingAd()) {
if (isPlayingAd() && controllerHideDuringAds) {
hideController();
}
}
Expand Down
1 change: 1 addition & 0 deletions library/ui/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<attr name="default_artwork" format="reference"/>
<attr name="use_controller" format="boolean"/>
<attr name="hide_on_touch" format="boolean"/>
<attr name="hide_during_ads" format="boolean"/>
<attr name="auto_show" format="boolean"/>
<attr name="resize_mode"/>
<attr name="surface_type"/>
Expand Down

0 comments on commit 3e9c86f

Please sign in to comment.