diff --git a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsSampleStreamWrapper.java b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsSampleStreamWrapper.java index 434b6c20114..81f57ac171f 100644 --- a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsSampleStreamWrapper.java +++ b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsSampleStreamWrapper.java @@ -270,14 +270,32 @@ public boolean selectTracks(TrackSelection[] selections, boolean[] mayRetainStre SampleStream[] streams, boolean[] streamResetFlags, long positionUs, boolean forceReset) { Assertions.checkState(prepared); int oldEnabledTrackGroupCount = enabledTrackGroupCount; - // Deselect old tracks. + + // Get the old (i.e. current before the loop below executes) primary track selection. The new + // primary selection will equal the old one unless it's changed in the loop. + TrackSelection oldPrimaryTrackSelection = chunkSource.getTrackSelection(); + TrackSelection primaryTrackSelection = oldPrimaryTrackSelection; + for (int i = 0; i < selections.length; i++) { - if (streams[i] != null && (selections[i] == null || !mayRetainStreamFlags[i])) { + TrackSelection selection = selections[i]; + + // Deselect old tracks, discarding the sample stream unless mayRetainStream is set + if (streams[i] != null && (selection == null || !mayRetainStreamFlags[i])) { enabledTrackGroupCount--; ((HlsSampleStream) streams[i]).unbindSampleQueue(); streams[i] = null; } + + // update the possibly changed reference to the track selection for the primary track group + if (selection != null) { + int trackGroupIndex = trackGroups.indexOf(selection.getTrackGroup()); + if (trackGroupIndex == primaryTrackGroupIndex) { + primaryTrackSelection = selection; + chunkSource.selectTracks(selection); + } + } } + // We'll always need to seek if we're being forced to reset, or if this is a first selection to // a position other than the one we started preparing with, or if we're making a selection // having previously disabled all tracks. @@ -286,20 +304,12 @@ public boolean selectTracks(TrackSelection[] selections, boolean[] mayRetainStre || (seenFirstTrackSelection ? oldEnabledTrackGroupCount == 0 : positionUs != lastSeekPositionUs); - // Get the old (i.e. current before the loop below executes) primary track selection. The new - // primary selection will equal the old one unless it's changed in the loop. - TrackSelection oldPrimaryTrackSelection = chunkSource.getTrackSelection(); - TrackSelection primaryTrackSelection = oldPrimaryTrackSelection; + // Select new tracks. for (int i = 0; i < selections.length; i++) { if (streams[i] == null && selections[i] != null) { enabledTrackGroupCount++; - TrackSelection selection = selections[i]; - int trackGroupIndex = trackGroups.indexOf(selection.getTrackGroup()); - if (trackGroupIndex == primaryTrackGroupIndex) { - primaryTrackSelection = selection; - chunkSource.selectTracks(selection); - } + int trackGroupIndex = trackGroups.indexOf(selections[i].getTrackGroup()); streams[i] = new HlsSampleStream(this, trackGroupIndex); streamResetFlags[i] = true; if (trackGroupToSampleQueueIndex != null) {