androidx / androidx/media

How to properly force seekToNextMediaItem during advert playing

Open
#2,748 3 comments 0 reactions 1 assignee View on GitHub

@marcbaechinger is already working on this.

Since Aug 28, 2025.

question
Dominant language
Java
Stars
3k
Forks
955
Avg merge
12d 14h
Merged PRs (30d)
2

Description

We developing video-based application where user can watch different videos with built-in media3 advert support feature. We need to implement feature, to switch video (as user wants) even when advert is playing - player.seekToNextMediaItem.

The main problem is that it's impossible natively:
androidx\media3\exoplayer\ExoPlayerImpl.java#871 (code snippet for media3 1.7.1(same as 1.6.1)):

protected void seekTo(...) {
    ...
    if (isPlayingAd()) {
      // TODO: Investigate adding support for seeking during ads. This is complicated to do in
      // general because the midroll ad preceding the seek destination must be played before the
      // content position can be played, if a different ad is playing at the moment.
      Log.w(TAG, "seekTo ignored because an ad is playing");
      ...
      return;
    }
}

We have investigated possible ways to "drop ads" from the current video so that it is possible to switch to the next video:

Our solution 1 (force re-create media item):

for example: player is playing now VideoA, we need to switch to videoB, but we need to save playlist (i.e user can seek back from videoB to videoA)

val videoB = MediaItem.fromUri(<any mp4 video url>)
player.stop()

// Add target video next to current
val currentIndex = player.currentMediaItemIndex
player.addMediaItem(videoB, currentIndex + 1)

val videoA = player.currentMediaItem
// Remove current media item to drop ads (player.replaceMediaItem(currentIndex, videoA) not working here)
player.removeMediaItem(currentIndex)
// ~~ here player will auto-switch to videoB ~~
// return videoA to playlist
player.addMediaItem(videoA, currentIndex)

player.prepare()
Our solution 2 (force skip ads, wait and seek):
val action = {
    val videoB = MediaItem.fromUri(<any mp4 video url>)
    player.addMediaItem(videoB)
    player.seekToNextMediaItem()
}

// Simplified just for reference whithout unnecessary details
AdsLoader.EventListener.onAdPlaybackState(adPlaybackState.withSkippedAdGroup(<curentAdGroupIndex>))

// Simplified code of player listener without unnecessary details. When advert will really dropped by skipped adPlaybackState, this callback will be called
override fun onPositionDiscontinuity(...) {
    if (!player.isPlayingAd) {
        action.invoke()
    }
}

All of these methods are working, but they are ugly and solution 2 is working with delay ~300ms which is poor for UX.
Are there any better ways to switch video during ads playing?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.