Sam Chen
Sam Chen

Reputation: 8857

Android ExoPlayer PlayList Pause Current Track When It Finishes

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.

I've tried using onPositionDiscontinuity() but it is called after the track has changed to the next one.

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
    }
}

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

Upvotes: 1

Views: 1897

Answers (2)

artenson.art98
artenson.art98

Reputation: 1605

You can use the setPauseAtEndOfMediaItems method available on SimpleExoplayer.Builder like so:

player = SimpleExoPlayer.Builder(context)
            .setPauseAtEndOfMediaItems(true)
            .yourOtherOptions()
            .build()

Upvotes: 1

Akki
Akki

Reputation: 823

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. 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.

Upvotes: 0

Related Questions