Simplify handling of playback speed in StyledPlayerControlView

PiperOrigin-RevId: 360404403
This commit is contained in:
olly 2021-03-02 13:13:40 +00:00 committed by Oliver Woodman
parent 67b18a958e
commit 70f831a647

View File

@ -436,14 +436,10 @@ public class StyledPlayerControlView extends FrameLayout {
private StyledPlayerControlViewLayoutManager controlViewLayoutManager; private StyledPlayerControlViewLayoutManager controlViewLayoutManager;
private Resources resources; private Resources resources;
private int selectedMainSettingsPosition;
private RecyclerView settingsView; private RecyclerView settingsView;
private SettingsAdapter settingsAdapter; private SettingsAdapter settingsAdapter;
private SubSettingsAdapter subSettingsAdapter; private PlaybackSpeedAdapter playbackSpeedAdapter;
private PopupWindow settingsWindow; private PopupWindow settingsWindow;
private String[] playbackSpeedTexts;
private int[] playbackSpeedsMultBy100;
private int selectedPlaybackSpeedIndex;
private boolean needToHideBars; private boolean needToHideBars;
private int settingsWindowMargin; private int settingsWindowMargin;
@ -663,12 +659,7 @@ public class StyledPlayerControlView extends FrameLayout {
settingIcons[SETTINGS_AUDIO_TRACK_SELECTION_POSITION] = settingIcons[SETTINGS_AUDIO_TRACK_SELECTION_POSITION] =
resources.getDrawable(R.drawable.exo_styled_controls_audiotrack); resources.getDrawable(R.drawable.exo_styled_controls_audiotrack);
settingsAdapter = new SettingsAdapter(settingTexts, settingIcons); settingsAdapter = new SettingsAdapter(settingTexts, settingIcons);
playbackSpeedTexts = resources.getStringArray(R.array.exo_playback_speeds);
playbackSpeedsMultBy100 = resources.getIntArray(R.array.exo_speed_multiplied_by_100);
settingsWindowMargin = resources.getDimensionPixelSize(R.dimen.exo_settings_offset); settingsWindowMargin = resources.getDimensionPixelSize(R.dimen.exo_settings_offset);
subSettingsAdapter = new SubSettingsAdapter();
settingsView = settingsView =
(RecyclerView) (RecyclerView)
LayoutInflater.from(context).inflate(R.layout.exo_styled_settings_list, null); LayoutInflater.from(context).inflate(R.layout.exo_styled_settings_list, null);
@ -693,6 +684,10 @@ public class StyledPlayerControlView extends FrameLayout {
resources.getString(R.string.exo_controls_cc_disabled_description); resources.getString(R.string.exo_controls_cc_disabled_description);
textTrackSelectionAdapter = new TextTrackSelectionAdapter(); textTrackSelectionAdapter = new TextTrackSelectionAdapter();
audioTrackSelectionAdapter = new AudioTrackSelectionAdapter(); audioTrackSelectionAdapter = new AudioTrackSelectionAdapter();
playbackSpeedAdapter =
new PlaybackSpeedAdapter(
resources.getStringArray(R.array.exo_playback_speeds),
resources.getIntArray(R.array.exo_speed_multiplied_by_100));
fullScreenExitDrawable = resources.getDrawable(R.drawable.exo_styled_controls_fullscreen_exit); fullScreenExitDrawable = resources.getDrawable(R.drawable.exo_styled_controls_fullscreen_exit);
fullScreenEnterDrawable = fullScreenEnterDrawable =
@ -770,7 +765,6 @@ public class StyledPlayerControlView extends FrameLayout {
this.trackSelector = null; this.trackSelector = null;
} }
updateAll(); updateAll();
updateSettingsPlaybackSpeedLists();
} }
/** /**
@ -1102,6 +1096,7 @@ public class StyledPlayerControlView extends FrameLayout {
updateRepeatModeButton(); updateRepeatModeButton();
updateShuffleButton(); updateShuffleButton();
updateTrackLists(); updateTrackLists();
updatePlaybackSpeedList();
updateTimeline(); updateTimeline();
} }
@ -1439,24 +1434,13 @@ public class StyledPlayerControlView extends FrameLayout {
} }
} }
private void updateSettingsPlaybackSpeedLists() { private void updatePlaybackSpeedList() {
if (player == null) { if (player == null) {
return; return;
} }
float speed = player.getPlaybackParameters().speed; playbackSpeedAdapter.updateSelectedIndex(player.getPlaybackParameters().speed);
int currentSpeedMultBy100 = Math.round(speed * 100);
int closestMatchIndex = 0;
int closestMatchDifference = Integer.MAX_VALUE;
for (int i = 0; i < playbackSpeedsMultBy100.length; i++) {
int difference = Math.abs(currentSpeedMultBy100 - playbackSpeedsMultBy100[i]);
if (difference < closestMatchDifference) {
closestMatchIndex = i;
closestMatchDifference = difference;
}
}
selectedPlaybackSpeedIndex = closestMatchIndex;
settingsAdapter.setSubTextAtPosition( settingsAdapter.setSubTextAtPosition(
SETTINGS_PLAYBACK_SPEED_POSITION, playbackSpeedTexts[closestMatchIndex]); SETTINGS_PLAYBACK_SPEED_POSITION, playbackSpeedAdapter.getSelectedText());
} }
private void updateSettingsWindowSize() { private void updateSettingsWindowSize() {
@ -1572,27 +1556,14 @@ public class StyledPlayerControlView extends FrameLayout {
private void onSettingViewClicked(int position) { private void onSettingViewClicked(int position) {
if (position == SETTINGS_PLAYBACK_SPEED_POSITION) { if (position == SETTINGS_PLAYBACK_SPEED_POSITION) {
subSettingsAdapter.init(playbackSpeedTexts, selectedPlaybackSpeedIndex); displaySettingsWindow(playbackSpeedAdapter);
selectedMainSettingsPosition = SETTINGS_PLAYBACK_SPEED_POSITION;
displaySettingsWindow(subSettingsAdapter);
} else if (position == SETTINGS_AUDIO_TRACK_SELECTION_POSITION) { } else if (position == SETTINGS_AUDIO_TRACK_SELECTION_POSITION) {
selectedMainSettingsPosition = SETTINGS_AUDIO_TRACK_SELECTION_POSITION;
displaySettingsWindow(audioTrackSelectionAdapter); displaySettingsWindow(audioTrackSelectionAdapter);
} else { } else {
settingsWindow.dismiss(); settingsWindow.dismiss();
} }
} }
private void onSubSettingViewClicked(int position) {
if (selectedMainSettingsPosition == SETTINGS_PLAYBACK_SPEED_POSITION) {
if (position != selectedPlaybackSpeedIndex) {
float speed = playbackSpeedsMultBy100[position] / 100.0f;
setPlaybackSpeed(speed);
}
}
settingsWindow.dismiss();
}
private void onLayoutChange( private void onLayoutChange(
View v, View v,
int left, int left,
@ -1837,7 +1808,7 @@ public class StyledPlayerControlView extends FrameLayout {
updateTimeline(); updateTimeline();
} }
if (events.contains(EVENT_PLAYBACK_PARAMETERS_CHANGED)) { if (events.contains(EVENT_PLAYBACK_PARAMETERS_CHANGED)) {
updateSettingsPlaybackSpeedLists(); updatePlaybackSpeedList();
} }
if (events.contains(EVENT_TRACKS_CHANGED)) { if (events.contains(EVENT_TRACKS_CHANGED)) {
updateTrackLists(); updateTrackLists();
@ -1951,18 +1922,33 @@ public class StyledPlayerControlView extends FrameLayout {
} }
} }
private class SubSettingsAdapter extends RecyclerView.Adapter<SubSettingViewHolder> { private final class PlaybackSpeedAdapter extends RecyclerView.Adapter<SubSettingViewHolder> {
private String[] texts; private final String[] playbackSpeedTexts;
private final int[] playbackSpeedsMultBy100;
private int selectedIndex; private int selectedIndex;
public SubSettingsAdapter() { public PlaybackSpeedAdapter(String[] playbackSpeedTexts, int[] playbackSpeedsMultBy100) {
texts = new String[0]; this.playbackSpeedTexts = playbackSpeedTexts;
this.playbackSpeedsMultBy100 = playbackSpeedsMultBy100;
} }
public void init(String[] texts, int selectedIndex) { public void updateSelectedIndex(float playbackSpeed) {
this.texts = texts; int currentSpeedMultBy100 = Math.round(playbackSpeed * 100);
this.selectedIndex = selectedIndex; int closestMatchIndex = 0;
int closestMatchDifference = Integer.MAX_VALUE;
for (int i = 0; i < playbackSpeedsMultBy100.length; i++) {
int difference = Math.abs(currentSpeedMultBy100 - playbackSpeedsMultBy100[i]);
if (difference < closestMatchDifference) {
closestMatchIndex = i;
closestMatchDifference = difference;
}
}
selectedIndex = closestMatchIndex;
}
public String getSelectedText() {
return playbackSpeedTexts[selectedIndex];
} }
@Override @Override
@ -1975,27 +1961,23 @@ public class StyledPlayerControlView extends FrameLayout {
@Override @Override
public void onBindViewHolder(SubSettingViewHolder holder, int position) { public void onBindViewHolder(SubSettingViewHolder holder, int position) {
if (position < texts.length) { if (position < playbackSpeedTexts.length) {
holder.textView.setText(texts[position]); holder.textView.setText(playbackSpeedTexts[position]);
} }
holder.checkView.setVisibility(position == selectedIndex ? VISIBLE : INVISIBLE); holder.checkView.setVisibility(position == selectedIndex ? VISIBLE : INVISIBLE);
holder.itemView.setOnClickListener(
v -> {
if (position != selectedIndex) {
float speed = playbackSpeedsMultBy100[position] / 100.0f;
setPlaybackSpeed(speed);
}
settingsWindow.dismiss();
});
} }
@Override @Override
public int getItemCount() { public int getItemCount() {
return texts.length; return playbackSpeedTexts.length;
}
}
private final class SubSettingViewHolder extends RecyclerView.ViewHolder {
private final TextView textView;
private final View checkView;
public SubSettingViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.exo_text);
checkView = itemView.findViewById(R.id.exo_check);
itemView.setOnClickListener(v -> onSubSettingViewClicked(getAdapterPosition()));
} }
} }
@ -2043,7 +2025,7 @@ public class StyledPlayerControlView extends FrameLayout {
} }
@Override @Override
public void onBindViewHolderAtZeroPosition(TrackSelectionViewHolder holder) { public void onBindViewHolderAtZeroPosition(SubSettingViewHolder holder) {
// CC options include "Off" at the first position, which disables text rendering. // CC options include "Off" at the first position, which disables text rendering.
holder.textView.setText(R.string.exo_track_selection_none); holder.textView.setText(R.string.exo_track_selection_none);
boolean isTrackSelectionOff = true; boolean isTrackSelectionOff = true;
@ -2072,7 +2054,7 @@ public class StyledPlayerControlView extends FrameLayout {
} }
@Override @Override
public void onBindViewHolder(TrackSelectionViewHolder holder, int position) { public void onBindViewHolder(SubSettingViewHolder holder, int position) {
super.onBindViewHolder(holder, position); super.onBindViewHolder(holder, position);
if (position > 0) { if (position > 0) {
TrackInfo track = tracks.get(position - 1); TrackInfo track = tracks.get(position - 1);
@ -2089,7 +2071,7 @@ public class StyledPlayerControlView extends FrameLayout {
private final class AudioTrackSelectionAdapter extends TrackSelectionAdapter { private final class AudioTrackSelectionAdapter extends TrackSelectionAdapter {
@Override @Override
public void onBindViewHolderAtZeroPosition(TrackSelectionViewHolder holder) { public void onBindViewHolderAtZeroPosition(SubSettingViewHolder holder) {
// Audio track selection option includes "Auto" at the top. // Audio track selection option includes "Auto" at the top.
holder.textView.setText(R.string.exo_track_selection_auto); holder.textView.setText(R.string.exo_track_selection_auto);
// hasSelectionOverride is true means there is an explicit track selection, not "Auto". // hasSelectionOverride is true means there is an explicit track selection, not "Auto".
@ -2168,8 +2150,7 @@ public class StyledPlayerControlView extends FrameLayout {
} }
} }
private abstract class TrackSelectionAdapter private abstract class TrackSelectionAdapter extends RecyclerView.Adapter<SubSettingViewHolder> {
extends RecyclerView.Adapter<TrackSelectionViewHolder> {
protected List<Integer> rendererIndices; protected List<Integer> rendererIndices;
protected List<TrackInfo> tracks; protected List<TrackInfo> tracks;
@ -2185,19 +2166,19 @@ public class StyledPlayerControlView extends FrameLayout {
List<Integer> rendererIndices, List<TrackInfo> trackInfos, MappedTrackInfo mappedTrackInfo); List<Integer> rendererIndices, List<TrackInfo> trackInfos, MappedTrackInfo mappedTrackInfo);
@Override @Override
public TrackSelectionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public SubSettingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = View v =
LayoutInflater.from(getContext()) LayoutInflater.from(getContext())
.inflate(R.layout.exo_styled_sub_settings_list_item, null); .inflate(R.layout.exo_styled_sub_settings_list_item, null);
return new TrackSelectionViewHolder(v); return new SubSettingViewHolder(v);
} }
public abstract void onBindViewHolderAtZeroPosition(TrackSelectionViewHolder holder); public abstract void onBindViewHolderAtZeroPosition(SubSettingViewHolder holder);
public abstract void onTrackSelection(String subtext); public abstract void onTrackSelection(String subtext);
@Override @Override
public void onBindViewHolder(TrackSelectionViewHolder holder, int position) { public void onBindViewHolder(SubSettingViewHolder holder, int position) {
if (trackSelector == null || mappedTrackInfo == null) { if (trackSelector == null || mappedTrackInfo == null) {
return; return;
} }
@ -2253,12 +2234,12 @@ public class StyledPlayerControlView extends FrameLayout {
} }
} }
private static class TrackSelectionViewHolder extends RecyclerView.ViewHolder { private static class SubSettingViewHolder extends RecyclerView.ViewHolder {
public final TextView textView; public final TextView textView;
public final View checkView; public final View checkView;
public TrackSelectionViewHolder(View itemView) { public SubSettingViewHolder(View itemView) {
super(itemView); super(itemView);
textView = itemView.findViewById(R.id.exo_text); textView = itemView.findViewById(R.id.exo_text);
checkView = itemView.findViewById(R.id.exo_check); checkView = itemView.findViewById(R.id.exo_check);