【问题标题】:Display the remaining duration of a video in Exoplayer在 Exoplayer 中显示视频的剩余时长
【发布时间】:2017-10-25 09:30:47
【问题描述】:
我正在使用SimpleExoPlayer 使用默认控件播放原始文件夹中的视频。一切正常,但是,我想在控制栏的右上角显示剩余的持续时间而不是总持续时间。我可以使用播放器的getDuration 方法获取总时长,使用player.getDuration() - player.getCurrentPosition() 获取剩余时长。但我不知道如何每秒更新剩余持续时间并显示在视图上。
我怎样才能做到这一点?
【问题讨论】:
标签:
android
android-video-player
exoplayer
【解决方案1】:
这是一个如何实现每秒更新进度的简单示例。
在播放器未播放时(您可以从 ExoPlayer.addListener(Player.EventListener); 获取事件)以及您的 Activity 暂停/销毁时,您仍应使用 Handler.removeCallbacks(updateProgressAction)。
public class MainActivity extends AppCompatActivity {
private final Runnable updateProgressAction = new Runnable() {
@Override
public void run() {
updateProgress();
}
};
private TextView mTextView;
private Handler mHandler;
private SimpleExoPlayer mSimpleExoPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = findViewById(R.id.my_text);
SimpleExoPlayerView simpleExoPlayerView = findViewById(R.id.player_view);
mSimpleExoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(new DefaultBandwidthMeter()));
simpleExoPlayerView.setPlayer(mSimpleExoPlayer);
Uri uri = Uri.parse("https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8");
String userAgent = Util.getUserAgent(this, "appName");
HlsMediaSource mediaSource = new HlsMediaSource(uri, new DefaultDataSourceFactory(this, userAgent), null, null);
mSimpleExoPlayer.prepare(mediaSource);
mSimpleExoPlayer.setPlayWhenReady(true);
mHandler = new Handler();
mHandler.post(updateProgressAction);
}
private void updateProgress() {
mTextView.setText("calculate time and format");
long delayMs = TimeUnit.SECONDS.toMillis(1);
mHandler.postDelayed(updateProgressAction, delayMs);
}
}