ExoPlayer icon indicating copy to clipboard operation
ExoPlayer copied to clipboard

Play ads repeatedly for media items that are repeated

Open jenlai1345 opened this issue 3 years ago • 2 comments
trafficstars

Base on https://exoplayer.dev/ad-insertion.html, " If a media item is repeated, the user will see the corresponding ads only once (the ad playback state stores whether ads have been played, so they are skipped after their first occurrence)."

Problem: I have a playlist of 5 videos that are looped forever. I want to play ad (using ad_tag) after every video is played. Due to exoplayer's design, ad will play ONLY ONCE for every media item.

Help: Is there any way I can tell it to play ads despite it's the same media again? (I tried hacking ads playback state but no luck).

Thank you for all the comments and help out there!

This is how I build the playlist, passing in the same adTagUri for each video:

for (Object videoObject: videoObjects) {
                   MediaItem item =
                            new MediaItem.Builder()
                                    .setUri(videoObject.getUrl())
                                    .setMediaId(videoObject.getObjectId())
                                    .setAdsConfiguration(
                                            new MediaItem.AdsConfiguration.Builder(adTagUri)
                                                    .build())
                                    .build();
                    player.addMediaItem(item);
}

jenlai1345 avatar Jul 15 '22 02:07 jenlai1345

Any update on this issue?

jenlai1345 avatar Aug 30 '22 08:08 jenlai1345

As you pointed out, this is working as intended because usually ads are only meant to be played once within a specific content. Imagine you play your media item almost to the end and then seek back - would you also expect the ads to be played again?

I can see your use case though and there are some potential workarounds:

  1. Instead of looping, you could add the same item multiple times to the playlist. This way they should be treated as separate items and request new ads. Not sure if that is compatible with your UI and other logic in the app.
  2. You can also try to wrap AdsMediaSource (e.g. using the newly added WrappingMediaSource) and modify the returned AdPlaybackState to mark ad groups as AVAILABLE (and not PLAYED) once the content starts repeating. Note that you can only remove the PLAYED marker once the repetition starts, otherwise the ad will likely be played immediately because the player thinks it still needs to be played.

I can also mark this issue as an enhancement, but it's unlikely we get around to change it soon.

tonihei avatar Aug 30 '22 08:08 tonihei