Android ExoPlayer onProgressChanged [英] Android ExoPlayer onProgressChanged
问题描述
如何监控 ExoPlayer
上的进度变化?
我试图实现一个隐藏的 MediaController
并覆盖 setOnSeekBarChangeListener
方法,但目前没有成功.我想知道是否有另一种方法可以收听 ExoPlayer
进度.
How can I monitor progress changes on ExoPlayer
?
I tried to implement a hidden MediaController
and overriding setOnSeekBarChangeListener
methods, but for now without success. I'm wondering if there is another way to listen to the ExoPlayer
progress.
推荐答案
我知道这个问题很老了.但是,我在实现 ExoPlayer
时就想到了这一点.这是为了帮助以后做同样事情的其他人:)
I know this question is very old. But, I landed on this while implementing ExoPlayer
. This is to help the others who do the same later on:)
所以,我按照以下方法来跟踪播放进度.这是在 ExoPlayer
Google Docs 中完成的方式.它可以根据需要工作.
So, I have followed the following methods to track progress of the playback. This is the way it is done in the ExoPlayer
Google Docs. It works as needed.
updateProgressBar()
是更新SeekBar
进度的函数:
updateProgressBar()
is the function to update the SeekBar
progress:
private void updateProgressBar() {
long duration = player == null ? 0 : player.getDuration();
long position = player == null ? 0 : player.getCurrentPosition();
if (!dragging) {
mSeekBar.setProgress(progressBarValue(position));
}
long bufferedPosition = player == null ? 0 : player.getBufferedPosition();
mSeekBar.setSecondaryProgress(progressBarValue(bufferedPosition));
// Remove scheduled updates.
handler.removeCallbacks(updateProgressAction);
// Schedule an update if necessary.
int playbackState = player == null ? Player.STATE_IDLE : player.getPlaybackState();
if (playbackState != Player.STATE_IDLE && playbackState != Player.STATE_ENDED) {
long delayMs;
if (player.getPlayWhenReady() && playbackState == Player.STATE_READY) {
delayMs = 1000 - (position % 1000);
if (delayMs < 200) {
delayMs += 1000;
}
} else {
delayMs = 1000;
}
handler.postDelayed(updateProgressAction, delayMs);
}
}
private final Runnable updateProgressAction = new Runnable() {
@Override
public void run() {
updateProgressBar();
}
};
我们在 updateProgressAction
中反复调用 updateProgressBar()
直到播放停止.每当状态发生变化时,都会第一次调用该函数.我们使用 removeCallbacks(Runnable runnable)
这样总有一个 updateProgressAction
需要关心.
We call updateProgressBar()
within updateProgressAction
repeatedly until the playback stops.
The function is called the first time whenever there is a state change. We use removeCallbacks(Runnable runnable)
so that there is always one updateProgressAction
to care about.
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
updateProgressBar();
}
希望这会有所帮助!
这篇关于Android ExoPlayer onProgressChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!