Skip to content

Commit

Permalink
Make play button behave differently in IDLE and ENDED states
Browse files Browse the repository at this point in the history
- In IDLE, the button will now call a preparer. This allows
  removal of the separate retry button from the demo app.
- In ENDED, the button will seek back to the default position
  and play.
- Behavior is made consistent with LeanbackPlayerAdapter.

Issue: #3689

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=182506855
  • Loading branch information
ojw28 committed Jan 23, 2018
1 parent 7cacbfe commit 4828f27
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 27 deletions.
5 changes: 4 additions & 1 deletion RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
periods.
* Replaced `ExoPlayer.sendMessages` with `ExoPlayer.createMessage` to allow
more customization of the message. Now supports setting a message delivery
playback position and/or a delivery handler.
playback position and/or a delivery handler
([#2189](https://github.com/google/ExoPlayer/issues/2189)).
* UI components:
* Generalized player and control views to allow them to bind with any
`Player`, and renamed them to `PlayerView` and `PlayerControlView`
respectively.
* Made `PlayerView`'s play button behave correctly when the player is ended
([#3689](https://github.com/google/ExoPlayer/issues/3689)), and call a
`PlaybackPreparer` when the player is idle.
* Buffering:
* Allow a back-buffer of media to be retained behind the current playback
position, for fast backward seeking. The back-buffer can be configured by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.PlaybackPreparer;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.drm.DefaultDrmSessionManager;
Expand Down Expand Up @@ -82,7 +83,7 @@

/** An activity that plays media using {@link SimpleExoPlayer}. */
public class PlayerActivity extends Activity
implements OnClickListener, PlayerControlView.VisibilityListener {
implements OnClickListener, PlaybackPreparer, PlayerControlView.VisibilityListener {

public static final String DRM_SCHEME_EXTRA = "drm_scheme";
public static final String DRM_LICENSE_URL = "drm_license_url";
Expand Down Expand Up @@ -114,7 +115,6 @@ public class PlayerActivity extends Activity
private PlayerView playerView;
private LinearLayout debugRootView;
private TextView debugTextView;
private Button retryButton;

private DataSource.Factory mediaDataSourceFactory;
private SimpleExoPlayer player;
Expand Down Expand Up @@ -152,8 +152,6 @@ public void onCreate(Bundle savedInstanceState) {
rootView.setOnClickListener(this);
debugRootView = findViewById(R.id.controls_root);
debugTextView = findViewById(R.id.debug_text_view);
retryButton = findViewById(R.id.retry_button);
retryButton.setOnClickListener(this);

playerView = findViewById(R.id.player_view);
playerView.setControllerVisibilityListener(this);
Expand Down Expand Up @@ -229,9 +227,7 @@ public boolean dispatchKeyEvent(KeyEvent event) {

@Override
public void onClick(View view) {
if (view == retryButton) {
initializePlayer();
} else if (view.getParent() == debugRootView) {
if (view.getParent() == debugRootView) {
MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
if (mappedTrackInfo != null) {
trackSelectionHelper.showSelectionDialog(
Expand All @@ -240,6 +236,13 @@ public void onClick(View view) {
}
}

// PlaybackControlView.PlaybackPreparer implementation

@Override
public void preparePlayback() {
initializePlayer();
}

// PlaybackControlView.VisibilityListener implementation

@Override
Expand Down Expand Up @@ -301,9 +304,10 @@ private void initializePlayer() {
player.addMetadataOutput(eventLogger);
player.addAudioDebugListener(eventLogger);
player.addVideoDebugListener(eventLogger);
player.setPlayWhenReady(shouldAutoPlay);

playerView.setPlayer(player);
player.setPlayWhenReady(shouldAutoPlay);
playerView.setPlaybackPreparer(this);
debugViewHelper = new DebugTextViewHelper(player, debugTextView);
debugViewHelper.start();
}
Expand Down Expand Up @@ -502,10 +506,6 @@ private void releaseAdsLoader() {

private void updateButtonVisibilities() {
debugRootView.removeAllViews();

retryButton.setVisibility(inErrorState ? View.VISIBLE : View.GONE);
debugRootView.addView(retryButton);

if (player == null) {
return;
}
Expand Down
10 changes: 1 addition & 9 deletions demos/main/src/main/res/layout/player_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone">

<Button android:id="@+id/retry_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/retry"
android:visibility="gone"/>

</LinearLayout>
android:visibility="gone"/>

</LinearLayout>

Expand Down
2 changes: 0 additions & 2 deletions demos/main/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

<string name="text">Text</string>

<string name="retry">Retry</string>

<string name="selection_disabled">Disabled</string>

<string name="selection_default">Default</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.android.exoplayer2.DefaultControlDispatcher;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
import com.google.android.exoplayer2.PlaybackPreparer;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Player.DiscontinuityReason;
import com.google.android.exoplayer2.Player.TimelineChangeReason;
Expand All @@ -50,6 +51,7 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter {
private final ComponentListener componentListener;
private final Runnable updateProgressRunnable;

private @Nullable PlaybackPreparer playbackPreparer;
private ControlDispatcher controlDispatcher;
private ErrorMessageProvider<? super ExoPlaybackException> errorMessageProvider;
private SurfaceHolderGlueHost surfaceHolderGlueHost;
Expand Down Expand Up @@ -82,6 +84,15 @@ public void run() {
};
}

/**
* Sets the {@link PlaybackPreparer}.
*
* @param playbackPreparer The {@link PlaybackPreparer}.
*/
public void setPlaybackPreparer(@Nullable PlaybackPreparer playbackPreparer) {
this.playbackPreparer = playbackPreparer;
}

/**
* Sets the {@link ControlDispatcher}.
*
Expand Down Expand Up @@ -165,7 +176,11 @@ public long getCurrentPosition() {

@Override
public void play() {
if (player.getPlaybackState() == Player.STATE_ENDED) {
if (player.getPlaybackState() == Player.STATE_IDLE) {
if (playbackPreparer != null) {
playbackPreparer.preparePlayback();
}
} else if (player.getPlaybackState() == Player.STATE_ENDED) {
controlDispatcher.dispatchSeekTo(player, player.getCurrentWindowIndex(), C.TIME_UNSET);
}
if (controlDispatcher.dispatchSetPlayWhenReady(player, true)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2;

/** Called to prepare a playback. */
public interface PlaybackPreparer {

/** Called to prepare a playback. */
void preparePlayback();
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.widget.TextView;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
import com.google.android.exoplayer2.PlaybackPreparer;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.util.Assertions;
Expand Down Expand Up @@ -217,6 +218,7 @@ public interface VisibilityListener {
private Player player;
private com.google.android.exoplayer2.ControlDispatcher controlDispatcher;
private VisibilityListener visibilityListener;
private @Nullable PlaybackPreparer playbackPreparer;

private boolean isAttachedToWindow;
private boolean showMultiWindowTimeBar;
Expand Down Expand Up @@ -431,6 +433,15 @@ public void setVisibilityListener(VisibilityListener listener) {
this.visibilityListener = listener;
}

/**
* Sets the {@link PlaybackPreparer}.
*
* @param playbackPreparer The {@link PlaybackPreparer}.
*/
public void setPlaybackPreparer(@Nullable PlaybackPreparer playbackPreparer) {
this.playbackPreparer = playbackPreparer;
}

/**
* Sets the {@link com.google.android.exoplayer2.ControlDispatcher}.
*
Expand Down Expand Up @@ -599,7 +610,7 @@ private void updatePlayPauseButton() {
return;
}
boolean requestPlayPauseFocus = false;
boolean playing = player != null && player.getPlayWhenReady();
boolean playing = isPlaying();
if (playButton != null) {
requestPlayPauseFocus |= playing && playButton.isFocused();
playButton.setVisibility(playing ? View.GONE : View.VISIBLE);
Expand Down Expand Up @@ -811,7 +822,7 @@ private void updateProgress() {
}

private void requestPlayPauseFocus() {
boolean playing = player != null && player.getPlayWhenReady();
boolean playing = isPlaying();
if (!playing && playButton != null) {
playButton.requestFocus();
} else if (playing && pauseButton != null) {
Expand Down Expand Up @@ -985,6 +996,13 @@ public boolean dispatchMediaKeyEvent(KeyEvent event) {
return true;
}

private boolean isPlaying() {
return player != null
&& player.getPlaybackState() != Player.STATE_ENDED
&& player.getPlaybackState() != Player.STATE_IDLE
&& player.getPlayWhenReady();
}

@SuppressLint("InlinedApi")
private static boolean isHandledMediaKey(int keyCode) {
return keyCode == KeyEvent.KEYCODE_MEDIA_FAST_FORWARD
Expand Down Expand Up @@ -1085,6 +1103,13 @@ public void onClick(View view) {
} else if (rewindButton == view) {
rewind();
} else if (playButton == view) {
if (player.getPlaybackState() == Player.STATE_IDLE) {
if (playbackPreparer != null) {
playbackPreparer.preparePlayback();
}
} else if (player.getPlaybackState() == Player.STATE_ENDED) {
controlDispatcher.dispatchSeekTo(player, player.getCurrentWindowIndex(), C.TIME_UNSET);
}
controlDispatcher.dispatchSetPlayWhenReady(player, true);
} else if (pauseButton == view) {
controlDispatcher.dispatchSetPlayWhenReady(player, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ControlDispatcher;
import com.google.android.exoplayer2.DefaultControlDispatcher;
import com.google.android.exoplayer2.PlaybackPreparer;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Player.DiscontinuityReason;
import com.google.android.exoplayer2.metadata.Metadata;
Expand Down Expand Up @@ -676,6 +677,16 @@ public void setControllerVisibilityListener(PlayerControlView.VisibilityListener
controller.setVisibilityListener(listener);
}

/**
* Sets the {@link PlaybackPreparer}.
*
* @param playbackPreparer The {@link PlaybackPreparer}.
*/
public void setPlaybackPreparer(@Nullable PlaybackPreparer playbackPreparer) {
Assertions.checkState(controller != null);
controller.setPlaybackPreparer(playbackPreparer);
}

/**
* Sets the {@link ControlDispatcher}.
*
Expand Down

0 comments on commit 4828f27

Please sign in to comment.