Commit 74a9bf09 by olly Committed by marcbaechinger

Simplify handling of playback speed in StyledPlayerControlView

PiperOrigin-RevId: 360404403
parent 14711d1f
...@@ -436,14 +436,10 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -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;
...@@ -675,12 +671,7 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -675,12 +671,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);
...@@ -705,6 +696,10 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -705,6 +696,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 =
...@@ -782,7 +777,6 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -782,7 +777,6 @@ public class StyledPlayerControlView extends FrameLayout {
this.trackSelector = null; this.trackSelector = null;
} }
updateAll(); updateAll();
updateSettingsPlaybackSpeedLists();
} }
/** /**
...@@ -1114,6 +1108,7 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -1114,6 +1108,7 @@ public class StyledPlayerControlView extends FrameLayout {
updateRepeatModeButton(); updateRepeatModeButton();
updateShuffleButton(); updateShuffleButton();
updateTrackLists(); updateTrackLists();
updatePlaybackSpeedList();
updateTimeline(); updateTimeline();
} }
...@@ -1449,24 +1444,13 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -1449,24 +1444,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() {
...@@ -1582,27 +1566,14 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -1582,27 +1566,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,
...@@ -1847,7 +1818,7 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -1847,7 +1818,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();
...@@ -1967,18 +1938,33 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -1967,18 +1938,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 updateSelectedIndex(float playbackSpeed) {
int currentSpeedMultBy100 = Math.round(playbackSpeed * 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;
}
}
selectedIndex = closestMatchIndex;
} }
public void init(String[] texts, int selectedIndex) { public String getSelectedText() {
this.texts = texts; return playbackSpeedTexts[selectedIndex];
this.selectedIndex = selectedIndex;
} }
@Override @Override
...@@ -1991,27 +1977,23 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -1991,27 +1977,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()));
} }
} }
...@@ -2059,7 +2041,7 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -2059,7 +2041,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;
...@@ -2088,7 +2070,7 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -2088,7 +2070,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);
...@@ -2105,7 +2087,7 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -2105,7 +2087,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".
...@@ -2184,8 +2166,7 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -2184,8 +2166,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;
...@@ -2201,19 +2182,19 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -2201,19 +2182,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;
} }
...@@ -2269,12 +2250,12 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -2269,12 +2250,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);
......
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