Make the Cast demo app absorb remote queue changes

Necessary in two scenarios:
+ When the demo app starts casting to a receiver app that already
  had a queue.
+ When two demo apps are connected to the same receiver app and
  both make modifications.

PiperOrigin-RevId: 230546851
This commit is contained in:
aquilescanta 2019-01-23 17:46:44 +00:00 committed by Oliver Woodman
parent d7b3820175
commit fd081d49c7
3 changed files with 24 additions and 32 deletions

View File

@ -63,7 +63,7 @@ import java.util.ArrayList;
private final SimpleExoPlayer exoPlayer; private final SimpleExoPlayer exoPlayer;
private final CastPlayer castPlayer; private final CastPlayer castPlayer;
private final ArrayList<MediaItem> mediaQueue; private final ArrayList<MediaItem> mediaQueue;
private final QueuePositionListener queuePositionListener; private final QueueChangesListener queueChangesListener;
private final ConcatenatingMediaSource concatenatingMediaSource; private final ConcatenatingMediaSource concatenatingMediaSource;
private boolean castMediaQueueCreationPending; private boolean castMediaQueueCreationPending;
@ -71,32 +71,21 @@ import java.util.ArrayList;
private Player currentPlayer; private Player currentPlayer;
/** /**
* @param queuePositionListener A {@link QueuePositionListener} for queue position changes. * Creates a new manager for {@link SimpleExoPlayer} and {@link CastPlayer}.
*
* @param queueChangesListener A {@link QueueChangesListener} for queue position changes.
* @param localPlayerView The {@link PlayerView} for local playback. * @param localPlayerView The {@link PlayerView} for local playback.
* @param castControlView The {@link PlayerControlView} to control remote playback. * @param castControlView The {@link PlayerControlView} to control remote playback.
* @param context A {@link Context}. * @param context A {@link Context}.
* @param castContext The {@link CastContext}. * @param castContext The {@link CastContext}.
*/ */
public static DefaultReceiverPlayerManager createPlayerManager( public DefaultReceiverPlayerManager(
QueuePositionListener queuePositionListener, QueueChangesListener queueChangesListener,
PlayerView localPlayerView, PlayerView localPlayerView,
PlayerControlView castControlView, PlayerControlView castControlView,
Context context, Context context,
CastContext castContext) { CastContext castContext) {
DefaultReceiverPlayerManager defaultReceiverPlayerManager = this.queueChangesListener = queueChangesListener;
new DefaultReceiverPlayerManager(
queuePositionListener, localPlayerView, castControlView, context, castContext);
defaultReceiverPlayerManager.init();
return defaultReceiverPlayerManager;
}
private DefaultReceiverPlayerManager(
QueuePositionListener queuePositionListener,
PlayerView localPlayerView,
PlayerControlView castControlView,
Context context,
CastContext castContext) {
this.queuePositionListener = queuePositionListener;
this.localPlayerView = localPlayerView; this.localPlayerView = localPlayerView;
this.castControlView = castControlView; this.castControlView = castControlView;
mediaQueue = new ArrayList<>(); mediaQueue = new ArrayList<>();
@ -113,6 +102,8 @@ import java.util.ArrayList;
castPlayer.addListener(this); castPlayer.addListener(this);
castPlayer.setSessionAvailabilityListener(this); castPlayer.setSessionAvailabilityListener(this);
castControlView.setPlayer(castPlayer); castControlView.setPlayer(castPlayer);
setCurrentPlayer(castPlayer.isCastSessionAvailable() ? castPlayer : exoPlayer);
} }
// Queue manipulation methods. // Queue manipulation methods.
@ -287,10 +278,6 @@ import java.util.ArrayList;
// Internal methods. // Internal methods.
private void init() {
setCurrentPlayer(castPlayer.isCastSessionAvailable() ? castPlayer : exoPlayer);
}
private void updateCurrentItemIndex() { private void updateCurrentItemIndex() {
int playbackState = currentPlayer.getPlaybackState(); int playbackState = currentPlayer.getPlaybackState();
maybeSetCurrentItemAndNotify( maybeSetCurrentItemAndNotify(
@ -372,7 +359,7 @@ import java.util.ArrayList;
if (this.currentItemIndex != currentItemIndex) { if (this.currentItemIndex != currentItemIndex) {
int oldIndex = this.currentItemIndex; int oldIndex = this.currentItemIndex;
this.currentItemIndex = currentItemIndex; this.currentItemIndex = currentItemIndex;
queuePositionListener.onQueuePositionChanged(oldIndex, currentItemIndex); queueChangesListener.onQueuePositionChanged(oldIndex, currentItemIndex);
} }
} }

View File

@ -49,7 +49,7 @@ import java.util.Collections;
* Cast extension. * Cast extension.
*/ */
public class MainActivity extends AppCompatActivity public class MainActivity extends AppCompatActivity
implements OnClickListener, PlayerManager.QueuePositionListener { implements OnClickListener, PlayerManager.QueueChangesListener {
private final MediaItem.Builder mediaItemBuilder; private final MediaItem.Builder mediaItemBuilder;
@ -121,8 +121,8 @@ public class MainActivity extends AppCompatActivity
switch (applicationId) { switch (applicationId) {
case CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID: case CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID:
playerManager = playerManager =
DefaultReceiverPlayerManager.createPlayerManager( new DefaultReceiverPlayerManager(
/* queuePositionListener= */ this, /* queueChangesListener= */ this,
localPlayerView, localPlayerView,
castControlView, castControlView,
/* context= */ this, /* context= */ this,
@ -162,7 +162,7 @@ public class MainActivity extends AppCompatActivity
.show(); .show();
} }
// PlayerManager.QueuePositionListener implementation. // PlayerManager.QueueChangesListener implementation.
@Override @Override
public void onQueuePositionChanged(int previousIndex, int newIndex) { public void onQueuePositionChanged(int previousIndex, int newIndex) {
@ -174,6 +174,11 @@ public class MainActivity extends AppCompatActivity
} }
} }
@Override
public void onQueueContentsExternallyChanged() {
mediaQueueListAdapter.notifyDataSetChanged();
}
// Internal methods. // Internal methods.
private View buildSampleListView() { private View buildSampleListView() {

View File

@ -22,14 +22,14 @@ import com.google.android.exoplayer2.ext.cast.MediaItem;
/** Manages the players in the Cast demo app. */ /** Manages the players in the Cast demo app. */
interface PlayerManager { interface PlayerManager {
/** Listener for changes in the media queue playback position. */ /** Listener for changes in the media queue. */
interface QueuePositionListener { interface QueueChangesListener {
/** /** Called when the currently played item of the media queue changes. */
* Called when the currently played item of the media queue changes.
*/
void onQueuePositionChanged(int previousIndex, int newIndex); void onQueuePositionChanged(int previousIndex, int newIndex);
/** Called when the media queue changes due to modifications not caused by this manager. */
void onQueueContentsExternallyChanged();
} }
/** Redirects the given {@code keyEvent} to the active player. */ /** Redirects the given {@code keyEvent} to the active player. */