Skip to content

Commit

Permalink
add top-level playlist API
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbaechinger authored and ojw28 committed Dec 18, 2019
1 parent 43bbc17 commit 36fa9d5
Show file tree
Hide file tree
Showing 23 changed files with 4,722 additions and 978 deletions.
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
([6773](https://github.com/google/ExoPlayer/issues/6773)).
* Suppress ProGuard warnings for compile-time `javax.annotation` package
([#6771](https://github.com/google/ExoPlayer/issues/6771)).
* Add playlist API ([#6161](https://github.com/google/ExoPlayer/issues/6161)).

### 2.11.0 (2019-12-11) ###

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import com.google.android.exoplayer2.offline.DownloadHelper;
import com.google.android.exoplayer2.offline.DownloadRequest;
import com.google.android.exoplayer2.source.BehindLiveWindowException;
import com.google.android.exoplayer2.source.ConcatenatingMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.MediaSourceFactory;
import com.google.android.exoplayer2.source.MergingMediaSource;
Expand Down Expand Up @@ -82,6 +81,8 @@
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.util.ArrayList;
import java.util.List;

/** An activity that plays media using {@link SimpleExoPlayer}. */
public class PlayerActivity extends AppCompatActivity
Expand Down Expand Up @@ -147,7 +148,7 @@ public class PlayerActivity extends AppCompatActivity

private DataSource.Factory dataSourceFactory;
private SimpleExoPlayer player;
private MediaSource mediaSource;
private List<MediaSource> mediaSources;
private DefaultTrackSelector trackSelector;
private DefaultTrackSelector.Parameters trackSelectorParameters;
private DebugTextViewHelper debugViewHelper;
Expand Down Expand Up @@ -349,12 +350,10 @@ public void onVisibilityChange(int visibility) {
private void initializePlayer() {
if (player == null) {
Intent intent = getIntent();

mediaSource = createTopLevelMediaSource(intent);
if (mediaSource == null) {
mediaSources = createTopLevelMediaSources(intent);
if (mediaSources.isEmpty()) {
return;
}

TrackSelection.Factory trackSelectionFactory;
String abrAlgorithm = intent.getStringExtra(ABR_ALGORITHM_EXTRA);
if (abrAlgorithm == null || ABR_ALGORITHM_DEFAULT.equals(abrAlgorithm)) {
Expand Down Expand Up @@ -395,13 +394,12 @@ private void initializePlayer() {
if (haveStartPosition) {
player.seekTo(startWindow, startPosition);
}
player.setMediaSource(mediaSource);
player.setMediaSources(mediaSources, /* resetPosition= */ !haveStartPosition);
player.prepare();
updateButtonVisibility();
}

@Nullable
private MediaSource createTopLevelMediaSource(Intent intent) {
private List<MediaSource> createTopLevelMediaSources(Intent intent) {
String action = intent.getAction();
boolean actionIsListView = ACTION_VIEW_LIST.equals(action);
if (!actionIsListView && !ACTION_VIEW.equals(action)) {
Expand Down Expand Up @@ -429,10 +427,10 @@ private MediaSource createTopLevelMediaSource(Intent intent) {
}
}

MediaSource[] mediaSources = new MediaSource[samples.length];
for (int i = 0; i < samples.length; i++) {
mediaSources[i] = createLeafMediaSource(samples[i]);
Sample.SubtitleInfo subtitleInfo = samples[i].subtitleInfo;
List<MediaSource> mediaSources = new ArrayList<>();
for (UriSample sample : samples) {
MediaSource mediaSource = createLeafMediaSource(sample);
Sample.SubtitleInfo subtitleInfo = sample.subtitleInfo;
if (subtitleInfo != null) {
Format subtitleFormat =
Format.createTextSampleFormat(
Expand All @@ -443,33 +441,30 @@ private MediaSource createTopLevelMediaSource(Intent intent) {
MediaSource subtitleMediaSource =
new SingleSampleMediaSource.Factory(dataSourceFactory)
.createMediaSource(subtitleInfo.uri, subtitleFormat, C.TIME_UNSET);
mediaSources[i] = new MergingMediaSource(mediaSources[i], subtitleMediaSource);
mediaSource = new MergingMediaSource(mediaSource, subtitleMediaSource);
}
mediaSources.add(mediaSource);
}
MediaSource mediaSource =
mediaSources.length == 1 ? mediaSources[0] : new ConcatenatingMediaSource(mediaSources);

if (seenAdsTagUri) {
if (seenAdsTagUri && mediaSources.size() == 1) {
Uri adTagUri = samples[0].adTagUri;
if (actionIsListView) {
showToast(R.string.unsupported_ads_in_concatenation);
if (!adTagUri.equals(loadedAdTagUri)) {
releaseAdsLoader();
loadedAdTagUri = adTagUri;
}
MediaSource adsMediaSource = createAdsMediaSource(mediaSources.get(0), adTagUri);
if (adsMediaSource != null) {
mediaSources.set(0, adsMediaSource);
} else {
if (!adTagUri.equals(loadedAdTagUri)) {
releaseAdsLoader();
loadedAdTagUri = adTagUri;
}
MediaSource adsMediaSource = createAdsMediaSource(mediaSource, adTagUri);
if (adsMediaSource != null) {
mediaSource = adsMediaSource;
} else {
showToast(R.string.ima_not_loaded);
}
showToast(R.string.ima_not_loaded);
}
} else if (seenAdsTagUri && mediaSources.size() > 1) {
showToast(R.string.unsupported_ads_in_concatenation);
releaseAdsLoader();
} else {
releaseAdsLoader();
}

return mediaSource;
return mediaSources;
}

private MediaSource createLeafMediaSource(UriSample parameters) {
Expand Down Expand Up @@ -557,7 +552,7 @@ private void releasePlayer() {
debugViewHelper = null;
player.release();
player = null;
mediaSource = null;
mediaSources = null;
trackSelector = null;
}
if (adsLoader != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public final class CastPlayer extends BasePlayer {
private int pendingSeekCount;
private int pendingSeekWindowIndex;
private long pendingSeekPositionMs;
private boolean waitingForInitialTimeline;

/**
* @param castContext The context from which the cast session is obtained.
Expand Down Expand Up @@ -173,7 +172,6 @@ public PendingResult<MediaChannelResult> loadItems(
MediaQueueItem[] items, int startIndex, long positionMs, @RepeatMode int repeatMode) {
if (remoteMediaClient != null) {
positionMs = positionMs != C.TIME_UNSET ? positionMs : 0;
waitingForInitialTimeline = true;
return remoteMediaClient.queueLoad(items, startIndex, getCastRepeatMode(repeatMode),
positionMs, null);
}
Expand Down Expand Up @@ -641,15 +639,13 @@ private void updateRepeatModeAndNotifyIfChanged(@Nullable ResultCallback<?> resu

private void updateTimelineAndNotifyIfChanged() {
if (updateTimeline()) {
@Player.TimelineChangeReason
int reason =
waitingForInitialTimeline
? Player.TIMELINE_CHANGE_REASON_PREPARED
: Player.TIMELINE_CHANGE_REASON_DYNAMIC;
waitingForInitialTimeline = false;
// TODO: Differentiate TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED and
// TIMELINE_CHANGE_REASON_SOURCE_UPDATE [see internal: b/65152553].
notificationsBatch.add(
new ListenerNotificationTask(
listener -> listener.onTimelineChanged(currentTimeline, reason)));
listener ->
listener.onTimelineChanged(
currentTimeline, Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public void onAdPlaybackState(AdPlaybackState adPlaybackState) {
this.adPlaybackState = adPlaybackState;
fakeExoPlayer.updateTimeline(
new SinglePeriodAdTimeline(contentTimeline, adPlaybackState),
Player.TIMELINE_CHANGE_REASON_DYNAMIC);
Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE);
}

@Override
Expand Down
Loading

0 comments on commit 36fa9d5

Please sign in to comment.