mirror of
https://github.com/androidx/media.git
synced 2025-05-07 23:50:44 +08:00
PlaybackControlView improvements
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=132981908
This commit is contained in:
parent
f2229d920b
commit
f4248410d9
@ -24,6 +24,7 @@ import android.widget.FrameLayout;
|
|||||||
import android.widget.ImageButton;
|
import android.widget.ImageButton;
|
||||||
import android.widget.SeekBar;
|
import android.widget.SeekBar;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import com.google.android.exoplayer2.C;
|
||||||
import com.google.android.exoplayer2.ExoPlaybackException;
|
import com.google.android.exoplayer2.ExoPlaybackException;
|
||||||
import com.google.android.exoplayer2.ExoPlayer;
|
import com.google.android.exoplayer2.ExoPlayer;
|
||||||
import com.google.android.exoplayer2.R;
|
import com.google.android.exoplayer2.R;
|
||||||
@ -53,6 +54,7 @@ public class PlaybackControlView extends FrameLayout {
|
|||||||
public static final int DEFAULT_REWIND_MS = 5000;
|
public static final int DEFAULT_REWIND_MS = 5000;
|
||||||
public static final int DEFAULT_SHOW_DURATION_MS = 5000;
|
public static final int DEFAULT_SHOW_DURATION_MS = 5000;
|
||||||
|
|
||||||
|
private static final int PROGRESS_BAR_MAX = 1000;
|
||||||
private static final long MAX_POSITION_FOR_SEEK_TO_PREVIOUS = 3000;
|
private static final long MAX_POSITION_FOR_SEEK_TO_PREVIOUS = 3000;
|
||||||
|
|
||||||
private final ComponentListener componentListener;
|
private final ComponentListener componentListener;
|
||||||
@ -72,7 +74,6 @@ public class PlaybackControlView extends FrameLayout {
|
|||||||
private VisibilityListener visibilityListener;
|
private VisibilityListener visibilityListener;
|
||||||
|
|
||||||
private boolean dragging;
|
private boolean dragging;
|
||||||
private boolean isProgressUpdating;
|
|
||||||
private int rewindMs = DEFAULT_REWIND_MS;
|
private int rewindMs = DEFAULT_REWIND_MS;
|
||||||
private int fastForwardMs = DEFAULT_FAST_FORWARD_MS;
|
private int fastForwardMs = DEFAULT_FAST_FORWARD_MS;
|
||||||
private int showDurationMs = DEFAULT_SHOW_DURATION_MS;
|
private int showDurationMs = DEFAULT_SHOW_DURATION_MS;
|
||||||
@ -80,12 +81,7 @@ public class PlaybackControlView extends FrameLayout {
|
|||||||
private final Runnable updateProgressAction = new Runnable() {
|
private final Runnable updateProgressAction = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
long position = updateProgress();
|
updateProgress();
|
||||||
if (!dragging && isVisible() && isPlaying()) {
|
|
||||||
postDelayed(updateProgressAction, 1000 - (position % 1000));
|
|
||||||
} else {
|
|
||||||
isProgressUpdating = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -109,14 +105,12 @@ public class PlaybackControlView extends FrameLayout {
|
|||||||
componentListener = new ComponentListener();
|
componentListener = new ComponentListener();
|
||||||
|
|
||||||
LayoutInflater.from(context).inflate(R.layout.playback_control_view, this);
|
LayoutInflater.from(context).inflate(R.layout.playback_control_view, this);
|
||||||
|
|
||||||
time = (TextView) findViewById(R.id.time);
|
time = (TextView) findViewById(R.id.time);
|
||||||
timeCurrent = (TextView) findViewById(R.id.time_current);
|
timeCurrent = (TextView) findViewById(R.id.time_current);
|
||||||
progressBar = (SeekBar) findViewById(R.id.mediacontroller_progress);
|
progressBar = (SeekBar) findViewById(R.id.mediacontroller_progress);
|
||||||
progressBar.setOnSeekBarChangeListener(componentListener);
|
progressBar.setOnSeekBarChangeListener(componentListener);
|
||||||
progressBar.setMax(1000);
|
progressBar.setMax(PROGRESS_BAR_MAX);
|
||||||
|
playButton = (ImageButton) findViewById(R.id.play);
|
||||||
playButton = (ImageButton) findViewById(R.id.pause);
|
|
||||||
playButton.setOnClickListener(componentListener);
|
playButton.setOnClickListener(componentListener);
|
||||||
previousButton = findViewById(R.id.prev);
|
previousButton = findViewById(R.id.prev);
|
||||||
previousButton.setOnClickListener(componentListener);
|
previousButton.setOnClickListener(componentListener);
|
||||||
@ -126,6 +120,7 @@ public class PlaybackControlView extends FrameLayout {
|
|||||||
rewindButton.setOnClickListener(componentListener);
|
rewindButton.setOnClickListener(componentListener);
|
||||||
fastForwardButton = findViewById(R.id.ffwd);
|
fastForwardButton = findViewById(R.id.ffwd);
|
||||||
fastForwardButton.setOnClickListener(componentListener);
|
fastForwardButton.setOnClickListener(componentListener);
|
||||||
|
updateAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -141,8 +136,7 @@ public class PlaybackControlView extends FrameLayout {
|
|||||||
if (player != null) {
|
if (player != null) {
|
||||||
player.addListener(componentListener);
|
player.addListener(componentListener);
|
||||||
}
|
}
|
||||||
updatePlayPauseButton();
|
updateAll();
|
||||||
updateTime();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -200,13 +194,9 @@ public class PlaybackControlView extends FrameLayout {
|
|||||||
if (visibilityListener != null) {
|
if (visibilityListener != null) {
|
||||||
visibilityListener.onVisibilityChange(getVisibility());
|
visibilityListener.onVisibilityChange(getVisibility());
|
||||||
}
|
}
|
||||||
isProgressUpdating = true;
|
updateAll();
|
||||||
post(updateProgressAction);
|
|
||||||
removeCallbacks(hideAction);
|
|
||||||
showDurationMs = durationMs;
|
showDurationMs = durationMs;
|
||||||
if (durationMs > 0) {
|
hideDeferred();
|
||||||
postDelayed(hideAction, durationMs);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -230,76 +220,99 @@ public class PlaybackControlView extends FrameLayout {
|
|||||||
|
|
||||||
private void hideDeferred() {
|
private void hideDeferred() {
|
||||||
removeCallbacks(hideAction);
|
removeCallbacks(hideAction);
|
||||||
if (showDurationMs != 0) {
|
if (showDurationMs > 0) {
|
||||||
postDelayed(hideAction, showDurationMs);
|
postDelayed(hideAction, showDurationMs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateAll() {
|
||||||
|
updatePlayPauseButton();
|
||||||
|
updateNavigation();
|
||||||
|
updateProgress();
|
||||||
|
}
|
||||||
|
|
||||||
private void updatePlayPauseButton() {
|
private void updatePlayPauseButton() {
|
||||||
playButton.setImageResource(player != null && player.getPlayWhenReady()
|
if (!isVisible()) {
|
||||||
? R.drawable.ic_media_pause : R.drawable.ic_media_play);
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
private void updateNavigationButtons() {
|
|
||||||
if (player.getCurrentTimeline() == null || player.getCurrentTimeline().getWindowCount() < 2) {
|
|
||||||
previousButton.setVisibility(GONE);
|
|
||||||
nextButton.setVisibility(GONE);
|
|
||||||
} else if (player.getCurrentWindowIndex() == 0) {
|
|
||||||
setButtonEnabled(previousButton, false);
|
|
||||||
setButtonEnabled(nextButton, true);
|
|
||||||
} else if (player.getCurrentWindowIndex() == player.getCurrentTimeline().getWindowCount() - 1) {
|
|
||||||
setButtonEnabled(previousButton, true);
|
|
||||||
setButtonEnabled(nextButton, false);
|
|
||||||
} else {
|
|
||||||
setButtonEnabled(previousButton, true);
|
|
||||||
setButtonEnabled(nextButton, true);
|
|
||||||
}
|
}
|
||||||
|
boolean playing = player != null && player.getPlayWhenReady();
|
||||||
|
playButton.setImageResource(playing ? R.drawable.ic_media_pause : R.drawable.ic_media_play);
|
||||||
|
playButton.setContentDescription(
|
||||||
|
getResources().getString(playing ? R.string.pause_description : R.string.play_description));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setButtonEnabled(View button, boolean enabled) {
|
private void updateNavigation() {
|
||||||
button.setEnabled(enabled);
|
if (!isVisible()) {
|
||||||
if (Util.SDK_INT >= 11) {
|
return;
|
||||||
button.setAlpha(enabled ? 1f : 0.3f);
|
|
||||||
button.setVisibility(VISIBLE);
|
|
||||||
} else {
|
|
||||||
button.setVisibility(enabled ? VISIBLE : INVISIBLE);
|
|
||||||
}
|
}
|
||||||
}
|
Timeline currentTimeline = player != null ? player.getCurrentTimeline() : null;
|
||||||
|
boolean haveTimeline = currentTimeline != null;
|
||||||
private void updateUiForLiveStream() {
|
boolean isSeekable = false;
|
||||||
int visibility = player.getCurrentTimeline() != null && player.getCurrentTimeline()
|
boolean enablePrevious = false;
|
||||||
.getWindow(player.getCurrentWindowIndex(), currentWindow).isDynamic ? GONE
|
boolean enableNext = false;
|
||||||
: VISIBLE;
|
if (haveTimeline) {
|
||||||
progressBar.setVisibility(visibility);
|
int currentWindowIndex = player.getCurrentWindowIndex();
|
||||||
timeCurrent.setVisibility(visibility);
|
currentTimeline.getWindow(currentWindowIndex, currentWindow);
|
||||||
time.setVisibility(visibility);
|
isSeekable = currentWindow.isSeekable;
|
||||||
fastForwardButton.setVisibility(visibility);
|
enablePrevious = currentWindowIndex > 0 || isSeekable || !currentWindow.isDynamic;
|
||||||
rewindButton.setVisibility(visibility);
|
enableNext = (currentWindowIndex < currentTimeline.getWindowCount() - 1)
|
||||||
}
|
|| currentWindow.isDynamic;
|
||||||
|
|
||||||
private long updateProgress() {
|
|
||||||
if (player == null || dragging) {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
long position = player.getCurrentPosition();
|
setButtonEnabled(enablePrevious , previousButton);
|
||||||
long duration = player.getDuration();
|
setButtonEnabled(enableNext, nextButton);
|
||||||
if (progressBar != null) {
|
setButtonEnabled(isSeekable, fastForwardButton);
|
||||||
if (duration > 0) {
|
setButtonEnabled(isSeekable, rewindButton);
|
||||||
progressBar.setProgress((int) (1000 * position / duration));
|
progressBar.setEnabled(isSeekable);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateProgress() {
|
||||||
|
if (!isVisible()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
long duration = player == null ? 0 : player.getDuration();
|
||||||
|
long position = player == null ? 0 : player.getCurrentPosition();
|
||||||
|
time.setText(stringForTime(duration));
|
||||||
|
if (!dragging) {
|
||||||
|
timeCurrent.setText(stringForTime(position));
|
||||||
|
}
|
||||||
|
if (!dragging) {
|
||||||
|
progressBar.setProgress(progressBarValue(position));
|
||||||
|
}
|
||||||
|
long bufferedPosition = player == null ? 0 : player.getBufferedPosition();
|
||||||
|
progressBar.setSecondaryProgress(progressBarValue(bufferedPosition));
|
||||||
|
// Remove scheduled updates.
|
||||||
|
removeCallbacks(updateProgressAction);
|
||||||
|
// Schedule an update if necessary.
|
||||||
|
int playbackState = player == null ? ExoPlayer.STATE_IDLE : player.getPlaybackState();
|
||||||
|
if (playbackState != ExoPlayer.STATE_IDLE && playbackState != ExoPlayer.STATE_ENDED) {
|
||||||
|
long delayMs;
|
||||||
|
if (player.getPlayWhenReady() && playbackState == ExoPlayer.STATE_READY) {
|
||||||
|
delayMs = 1000 - (position % 1000);
|
||||||
|
if (delayMs < 200) {
|
||||||
|
delayMs += 1000;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
delayMs = 1000;
|
||||||
}
|
}
|
||||||
progressBar.setSecondaryProgress(player.getBufferedPercentage() * 10);
|
postDelayed(updateProgressAction, delayMs);
|
||||||
}
|
}
|
||||||
updateTime();
|
|
||||||
return position;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateTime() {
|
private void setButtonEnabled(boolean enabled, View view) {
|
||||||
time.setText(stringForTime(player == null ? 0 : player.getDuration()));
|
view.setEnabled(enabled);
|
||||||
timeCurrent.setText(stringForTime(player == null ? 0 : player.getCurrentPosition()));
|
if (Util.SDK_INT >= 11) {
|
||||||
|
view.setAlpha(enabled ? 1f : 0.3f);
|
||||||
|
view.setVisibility(VISIBLE);
|
||||||
|
} else {
|
||||||
|
view.setVisibility(enabled ? VISIBLE : INVISIBLE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String stringForTime(long timeMs) {
|
private String stringForTime(long timeMs) {
|
||||||
long totalSeconds = timeMs / 1000;
|
if (timeMs == C.TIME_UNSET) {
|
||||||
|
timeMs = 0;
|
||||||
|
}
|
||||||
|
long totalSeconds = (timeMs + 500) / 1000;
|
||||||
long seconds = totalSeconds % 60;
|
long seconds = totalSeconds % 60;
|
||||||
long minutes = (totalSeconds / 60) % 60;
|
long minutes = (totalSeconds / 60) % 60;
|
||||||
long hours = totalSeconds / 3600;
|
long hours = totalSeconds / 3600;
|
||||||
@ -308,15 +321,26 @@ public class PlaybackControlView extends FrameLayout {
|
|||||||
: formatter.format("%02d:%02d", minutes, seconds).toString();
|
: formatter.format("%02d:%02d", minutes, seconds).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isPlaying() {
|
private int progressBarValue(long position) {
|
||||||
return player != null && player.getPlayWhenReady() && (player.getPlaybackState()
|
long duration = player == null ? C.TIME_UNSET : player.getDuration();
|
||||||
== ExoPlayer.STATE_READY || player.getPlaybackState() == ExoPlayer.STATE_BUFFERING);
|
return duration == C.TIME_UNSET || duration == 0 ? 0
|
||||||
|
: (int) ((position * PROGRESS_BAR_MAX) / duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
private long positionValue(int progress) {
|
||||||
|
long duration = player == null ? C.TIME_UNSET : player.getDuration();
|
||||||
|
return duration == C.TIME_UNSET ? 0 : ((duration * progress) / PROGRESS_BAR_MAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void previous() {
|
private void previous() {
|
||||||
|
Timeline currentTimeline = player.getCurrentTimeline();
|
||||||
|
if (currentTimeline == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
int currentWindowIndex = player.getCurrentWindowIndex();
|
int currentWindowIndex = player.getCurrentWindowIndex();
|
||||||
if (currentWindowIndex > 0 && player.getCurrentPosition()
|
currentTimeline.getWindow(currentWindowIndex, currentWindow);
|
||||||
<= MAX_POSITION_FOR_SEEK_TO_PREVIOUS) {
|
if (currentWindowIndex > 0 && (player.getCurrentPosition() <= MAX_POSITION_FOR_SEEK_TO_PREVIOUS
|
||||||
|
|| (currentWindow.isDynamic && !currentWindow.isSeekable))) {
|
||||||
player.seekToDefaultPosition(currentWindowIndex - 1);
|
player.seekToDefaultPosition(currentWindowIndex - 1);
|
||||||
} else {
|
} else {
|
||||||
player.seekTo(0);
|
player.seekTo(0);
|
||||||
@ -324,10 +348,15 @@ public class PlaybackControlView extends FrameLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void next() {
|
private void next() {
|
||||||
int currentWindowIndex = player.getCurrentWindowIndex();
|
|
||||||
Timeline currentTimeline = player.getCurrentTimeline();
|
Timeline currentTimeline = player.getCurrentTimeline();
|
||||||
if (currentTimeline != null && currentWindowIndex < currentTimeline.getWindowCount() - 1) {
|
if (currentTimeline == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int currentWindowIndex = player.getCurrentWindowIndex();
|
||||||
|
if (currentWindowIndex < currentTimeline.getWindowCount() - 1) {
|
||||||
player.seekToDefaultPosition(currentWindowIndex + 1);
|
player.seekToDefaultPosition(currentWindowIndex + 1);
|
||||||
|
} else if (currentTimeline.getWindow(currentWindowIndex, currentWindow, false).isDynamic) {
|
||||||
|
player.seekToDefaultPosition();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -386,37 +415,34 @@ public class PlaybackControlView extends FrameLayout {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||||
timeCurrent.setText(stringForTime(player == null ? 0 : player.getDuration() * progress
|
if (fromUser) {
|
||||||
/ 1000));
|
timeCurrent.setText(stringForTime(positionValue(progress)));
|
||||||
progressBar.setSecondaryProgress(player == null ? 0 : player.getBufferedPercentage() * 10);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStopTrackingTouch(SeekBar seekBar) {
|
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||||
dragging = false;
|
dragging = false;
|
||||||
player.seekTo(player.getDuration() * seekBar.getProgress() / 1000);
|
player.seekTo(positionValue(seekBar.getProgress()));
|
||||||
hideDeferred();
|
hideDeferred();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
|
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
|
||||||
if (isPlaying() && !isProgressUpdating) {
|
|
||||||
isProgressUpdating = true;
|
|
||||||
post(updateProgressAction);
|
|
||||||
}
|
|
||||||
updatePlayPauseButton();
|
updatePlayPauseButton();
|
||||||
|
updateProgress();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPositionDiscontinuity() {
|
public void onPositionDiscontinuity() {
|
||||||
updateNavigationButtons();
|
updateNavigation();
|
||||||
updateProgress();
|
updateProgress();
|
||||||
updateUiForLiveStream();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTimelineChanged(Timeline timeline, Object manifest) {
|
public void onTimelineChanged(Timeline timeline, Object manifest) {
|
||||||
// Do nothing.
|
updateNavigation();
|
||||||
|
updateProgress();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -36,9 +36,8 @@
|
|||||||
android:contentDescription="@string/rew_description"
|
android:contentDescription="@string/rew_description"
|
||||||
style="@style/MediaButton.Rew"/>
|
style="@style/MediaButton.Rew"/>
|
||||||
|
|
||||||
<ImageButton android:id="@+id/pause"
|
<ImageButton android:id="@+id/play"
|
||||||
android:contentDescription="@string/pause_description"
|
style="@style/MediaButton"/>
|
||||||
style="@style/MediaButton.Pause"/>
|
|
||||||
|
|
||||||
<ImageButton android:id="@+id/ffwd"
|
<ImageButton android:id="@+id/ffwd"
|
||||||
android:contentDescription="@string/ffw_description"
|
android:contentDescription="@string/ffw_description"
|
||||||
|
@ -31,11 +31,6 @@
|
|||||||
<item name="android:contentDescription">@string/next_description</item>
|
<item name="android:contentDescription">@string/next_description</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<style name="MediaButton.Play">
|
|
||||||
<item name="android:src">@drawable/ic_media_play</item>
|
|
||||||
<item name="android:contentDescription">@string/play_description</item>
|
|
||||||
</style>
|
|
||||||
|
|
||||||
<style name="MediaButton.Ffwd">
|
<style name="MediaButton.Ffwd">
|
||||||
<item name="android:src">@drawable/ic_media_ff</item>
|
<item name="android:src">@drawable/ic_media_ff</item>
|
||||||
<item name="android:contentDescription">@string/ffw_description</item>
|
<item name="android:contentDescription">@string/ffw_description</item>
|
||||||
@ -46,10 +41,5 @@
|
|||||||
<item name="android:contentDescription">@string/rew_description</item>
|
<item name="android:contentDescription">@string/rew_description</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<style name="MediaButton.Pause">
|
|
||||||
<item name="android:src">@drawable/ic_media_pause</item>
|
|
||||||
<item name="android:contentDescription">@string/pause_description</item>
|
|
||||||
</style>
|
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user