ExoPlayer
ExoPlayer copied to clipboard
Play ads repeatedly for media items that are repeated
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);
}
Any update on this issue?
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:
- 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.
- You can also try to wrap
AdsMediaSource(e.g. using the newly addedWrappingMediaSource) and modify the returnedAdPlaybackStateto 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.