Skip to content

Commit

Permalink
google#4304 - Add option to show buffering view when setPlayWhenReady…
Browse files Browse the repository at this point in the history
… is false
  • Loading branch information
szaboa committed Jul 26, 2018
1 parent 8952cca commit 1f12f22
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
Expand Down Expand Up @@ -61,6 +62,8 @@
import com.google.android.exoplayer2.util.RepeatModeUtil;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.video.VideoListener;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

/**
Expand Down Expand Up @@ -237,6 +240,14 @@ public class PlayerView extends FrameLayout {
private static final int SURFACE_TYPE_TEXTURE_VIEW = 2;
private static final int SURFACE_TYPE_MONO360_VIEW = 3;

public static final int SHOW_BUFFERING_NEVER = 0;
public static final int SHOW_BUFFERING_ALWAYS = 1;
public static final int SHOW_BUFFERING_WHEN_PLAYING = 2;

@IntDef({SHOW_BUFFERING_NEVER, SHOW_BUFFERING_WHEN_PLAYING, SHOW_BUFFERING_ALWAYS})
@Retention(RetentionPolicy.SOURCE)
public @interface ShowBuffering {}

private final AspectRatioFrameLayout contentFrame;
private final View shutterView;
private final View surfaceView;
Expand All @@ -252,7 +263,7 @@ public class PlayerView extends FrameLayout {
private boolean useController;
private boolean useArtwork;
private Bitmap defaultArtwork;
private boolean showBuffering;
private @ShowBuffering int showBuffering;
private boolean keepContentOnPlayerReset;
private @Nullable ErrorMessageProvider<? super ExoPlaybackException> errorMessageProvider;
private @Nullable CharSequence customErrorMessage;
Expand Down Expand Up @@ -306,7 +317,7 @@ public PlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
boolean controllerHideOnTouch = true;
boolean controllerAutoShow = true;
boolean controllerHideDuringAds = true;
boolean showBuffering = false;
int showBuffering = SHOW_BUFFERING_NEVER;
if (attrs != null) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PlayerView, 0, 0);
try {
Expand All @@ -324,7 +335,7 @@ public PlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
controllerHideOnTouch =
a.getBoolean(R.styleable.PlayerView_hide_on_touch, controllerHideOnTouch);
controllerAutoShow = a.getBoolean(R.styleable.PlayerView_auto_show, controllerAutoShow);
showBuffering = a.getBoolean(R.styleable.PlayerView_show_buffering, showBuffering);
showBuffering = a.getInteger(R.styleable.PlayerView_show_buffering, showBuffering);
keepContentOnPlayerReset =
a.getBoolean(
R.styleable.PlayerView_keep_content_on_player_reset, keepContentOnPlayerReset);
Expand Down Expand Up @@ -655,9 +666,28 @@ public void setKeepContentOnPlayerReset(boolean keepContentOnPlayerReset) {
* Sets whether a buffering spinner is displayed when the player is in the buffering state. The
* buffering spinner is not displayed by default.
*
* Deprecated, use {@link #setShowBuffering(int)}
*
* @param showBuffering Whether the buffering icon is displayed
*/
@Deprecated
public void setShowBuffering(boolean showBuffering) {
setShowBuffering(showBuffering ? SHOW_BUFFERING_ALWAYS : SHOW_BUFFERING_NEVER);
}

/**
* Sets whether a buffering spinner is displayed when the player is in the buffering state. The
* buffering spinner is not displayed by default (initial value {@link #SHOW_BUFFERING_NEVER})
*
* <p><ul>
* <li>{@link #SHOW_BUFFERING_ALWAYS} displayed always when buffering
* <li>{@link #SHOW_BUFFERING_NEVER} not displayed at all
* <li>{@link #SHOW_BUFFERING_WHEN_PLAYING} displayed only when playing and buffering
* </p></ul>
*
* @param showBuffering Buffering strategy that defines when the buffering icon is displayed
*/
public void setShowBuffering(@ShowBuffering int showBuffering) {
if (this.showBuffering != showBuffering) {
this.showBuffering = showBuffering;
updateBuffering();
Expand Down Expand Up @@ -1139,11 +1169,23 @@ private void closeShutter() {

private void updateBuffering() {
if (bufferingView != null) {
boolean showBufferingSpinner =
showBuffering
&& player != null
&& player.getPlaybackState() == Player.STATE_BUFFERING
&& player.getPlayWhenReady();

boolean showBufferingSpinner = false;

if (player != null && player.getPlaybackState() == Player.STATE_BUFFERING) {
switch (showBuffering) {
case SHOW_BUFFERING_ALWAYS:
showBufferingSpinner = true;
break;
case SHOW_BUFFERING_NEVER:
showBufferingSpinner = false;
break;
case SHOW_BUFFERING_WHEN_PLAYING:
showBufferingSpinner = player.getPlayWhenReady();
break;
}
}

bufferingView.setVisibility(showBufferingSpinner ? View.VISIBLE : View.GONE);
}
}
Expand Down
6 changes: 5 additions & 1 deletion library/ui/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@
<attr name="hide_on_touch" format="boolean"/>
<attr name="hide_during_ads" format="boolean"/>
<attr name="auto_show" format="boolean"/>
<attr name="show_buffering" format="boolean"/>
<attr name="show_buffering" format="enum">
<enum name="show_buffering_never" value="0"/>
<enum name="show_buffering_always" value="1"/>
<enum name="show_buffering_when_playing" value="2"/>
</attr>
<attr name="keep_content_on_player_reset" format="boolean"/>
<attr name="resize_mode"/>
<attr name="surface_type"/>
Expand Down

0 comments on commit 1f12f22

Please sign in to comment.