Commit 3b8eba2d by krocard Committed by Oliver Woodman

Refactor `initTrackSelectionAdapter`

As suggested in parent change, return a list of
`TrackType` instead of appending to it.

This has the slight disadvantage of iterating twice
over the (short) list, but clarifies the code.

PiperOrigin-RevId: 402844458
parent 98ee159d
...@@ -62,7 +62,6 @@ import com.google.android.exoplayer2.C; ...@@ -62,7 +62,6 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ControlDispatcher; import com.google.android.exoplayer2.ControlDispatcher;
import com.google.android.exoplayer2.DefaultControlDispatcher; import com.google.android.exoplayer2.DefaultControlDispatcher;
import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.ForwardingPlayer; import com.google.android.exoplayer2.ForwardingPlayer;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Player.Events; import com.google.android.exoplayer2.Player.Events;
...@@ -76,6 +75,7 @@ import com.google.android.exoplayer2.trackselection.TrackSelectionParameters.Tra ...@@ -76,6 +75,7 @@ import com.google.android.exoplayer2.trackselection.TrackSelectionParameters.Tra
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.RepeatModeUtil; import com.google.android.exoplayer2.util.RepeatModeUtil;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -1251,42 +1251,35 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -1251,42 +1251,35 @@ public class StyledPlayerControlView extends FrameLayout {
return; return;
} }
TracksInfo tracksInfo = player.getCurrentTracksInfo(); TracksInfo tracksInfo = player.getCurrentTracksInfo();
List<TrackGroupInfo> trackGroupInfos = tracksInfo.getTrackGroupInfos(); audioTrackSelectionAdapter.init(
if (trackGroupInfos.isEmpty()) { gatherSupportedTrackInfosOfType(tracksInfo, C.TRACK_TYPE_AUDIO));
return; if (controlViewLayoutManager.getShowButton(subtitleButton)) {
textTrackSelectionAdapter.init(
gatherSupportedTrackInfosOfType(tracksInfo, C.TRACK_TYPE_TEXT));
} else {
textTrackSelectionAdapter.init(ImmutableList.of());
} }
List<TrackInformation> textTracks = new ArrayList<>(); }
List<TrackInformation> audioTracks = new ArrayList<>();
private ImmutableList<TrackInformation> gatherSupportedTrackInfosOfType(
TracksInfo tracksInfo, @C.TrackType int trackType) {
ImmutableList.Builder<TrackInformation> tracks = new ImmutableList.Builder<>();
List<TrackGroupInfo> trackGroupInfos = tracksInfo.getTrackGroupInfos();
for (int trackGroupIndex = 0; trackGroupIndex < trackGroupInfos.size(); trackGroupIndex++) { for (int trackGroupIndex = 0; trackGroupIndex < trackGroupInfos.size(); trackGroupIndex++) {
TrackGroupInfo trackGroupInfo = trackGroupInfos.get(trackGroupIndex); TrackGroupInfo trackGroupInfo = trackGroupInfos.get(trackGroupIndex);
if (!trackGroupInfo.isSupported()) { if (trackGroupInfo.getTrackType() != trackType) {
continue; continue;
} }
if (trackGroupInfo.getTrackType() == C.TRACK_TYPE_TEXT TrackGroup trackGroup = trackGroupInfo.getTrackGroup();
&& controlViewLayoutManager.getShowButton(subtitleButton)) { for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
// Get TrackSelection at the corresponding renderer index. if (!trackGroupInfo.isTrackSupported(trackIndex)) {
gatherTrackInfosForAdapter(tracksInfo, trackGroupIndex, textTracks); continue;
} else if (trackGroupInfo.getTrackType() == C.TRACK_TYPE_AUDIO) { }
gatherTrackInfosForAdapter(tracksInfo, trackGroupIndex, audioTracks); String trackName = trackNameProvider.getTrackName(trackGroup.getFormat(trackIndex));
} tracks.add(new TrackInformation(tracksInfo, trackGroupIndex, trackIndex, trackName));
}
textTrackSelectionAdapter.init(textTracks);
audioTrackSelectionAdapter.init(audioTracks);
}
private void gatherTrackInfosForAdapter(
TracksInfo tracksInfo, int trackGroupIndex, List<TrackInformation> tracks) {
TrackGroupInfo trackGroupInfo = tracksInfo.getTrackGroupInfos().get(trackGroupIndex);
TrackGroup trackGroup = trackGroupInfo.getTrackGroup();
for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
Format format = trackGroup.getFormat(trackIndex);
if (trackGroupInfo.isTrackSupported(trackIndex)) {
tracks.add(
new TrackInformation(
tracksInfo, trackGroupIndex, trackIndex, trackNameProvider.getTrackName(format)));
} }
} }
return tracks.build();
} }
private void updateTimeline() { private void updateTimeline() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment