Android ExoPlayer PlayList完成后会暂停当前曲目 [英] Android ExoPlayer PlayList Pause Current Track When It Finishes

查看:113
本文介绍了Android ExoPlayer PlayList完成后会暂停当前曲目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在当前曲目结束后立即暂停它,但是播放列表播放的默认行为在整个播放列表完成之前不会暂停.

My goal is to pause the Current track right after it finishes, but the default behavior of playlist playback will not pause until the whole Playlist is finished.

我尝试使用 onPositionDiscontinuity() ,但是在曲目更改为下一个曲目后会调用它.

override fun onPositionDiscontinuity(reason: Int) {
    super.onPositionDiscontinuity(reason)

    if (reason == SimpleExoPlayer.DISCONTINUITY_REASON_PERIOD_TRANSITION) {
        Log.v("xxx", "called!")    //not called at the end of current track
    }
}

而且似乎不被本机支持(官方): https://github.com/google/ExoPlayer/issues/3773

And it seems like not supported natively (by official): https://github.com/google/ExoPlayer/issues/3773

推荐答案

不幸的是,没有直接的回调可用于通知当前轨道的最后一帧的结束.ConcatenatingMediaSource唯一已知的是轨道的结尾是 onPositionDiscontinuity(),但是正如您所知,只有在下一轨道的第一帧已经渲染之后才分派.因此,在那种情况下,我认为我们可以为您的用例提供以下可能性:

Unfortunately, there is no direct callback available to notify the end of the last frame of the current track. The only thing available with the ConcatenatingMediaSource, to know the end of a track is onPositionDiscontinuity(), but as you know that would be dispatched only after the first frame of the next track is already rendered. So in that case I think we can have the below possibilities wrt your use case:

  1. 使用 VideoFrameMetadataListener 接口并覆盖 onVideoFrameAboutToBeRendered(),当要渲染视频帧时,将在播放线程上调用该.在您的情况下,就在下一个轨道渲染之前.链接
  2. 获取曲目持续时间[ getDuration()],并在每秒钟(或任何其他时间间隔)内使用 getCurrentPosition()继续获取当前播放位置.并在返回指定时间时暂停播放.您可以为此使用 CountDownTimer ,并在计时器回调 onTick()中,为当前轨道调用getCurrentPosition().
  3. 使用 PlayerMessage 在指定的播放位置触发事件:可以使用PlayerMessage.setPosition设置应执行该播放位置的播放位置.
  1. Use VideoFrameMetadataListener interface and override the onVideoFrameAboutToBeRendered(), which would be called on the playback thread when a video frame is about to be rendered. In your case just before the next track rendering. link
  2. Get the track duration [getDuration()] and keep getting the current playback position using getCurrentPosition() in every second(or any other time interval). And pause the playback when it returns the specified time. You can use a CountDownTimer for this and in the timer callback, onTick(), invoke getCurrentPosition() for the current track. 
  3. Use PlayerMessage to fire events at specified playback positions: The playback position at which it should be executed can be set using PlayerMessage.setPosition.link
  4. Use onMediaItemTransition(): Called when playback transitions to another media item. Here generally we update the application’s UI for the new media item. So instead of updating the UI, we can pause the playback. Not sure if this gets called before or after onPositionDiscontinuity(). Feasibility needs to be verified.

这篇关于Android ExoPlayer PlayList完成后会暂停当前曲目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆