I’m using Exoplayer 3
along with the IMA SDK
. I need to show ads at specific times. Currently when I show the ads, they always appear at the beginning of the video.
I’m using the Exoplayer 3 IMA imports:
import androidx.media3.exoplayer.source.ads.AdsLoader;
import androidx.media3.exoplayer.source.ads.AdsMediaSource;
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory;
import androidx.media3.exoplayer.source.MediaSource;
import androidx.media3.exoplayer.ima.ImaAdsLoader;
I currently have AdsLoader
and DefaultMediaSourceFactory
integrated.
Does anyone know how I can show those ads at any time I want (for example at the 30th and 270th second)?
Edit-1:
I’ve actually managed to show ads at a certain point in time using this method:
private void showAd() {
long currentPosition = exoPlayer.getCurrentPosition();
//URL of the main video
MediaItem mediaItem = new MediaItem.Builder()
.setUri(Uri.parse(url))
.build();
MediaSource mediaSource = mediaSourceWithAdFactory.createMediaSource(mediaItem);
//IMA TAG EXAMPLE: PRE-ROLL
DataSpec dataSpec = new DataSpec(Uri.parse("https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/vmap_ad_samples&sz=640x480&cust_params=sample_ar%3Dpremidpost&ciu_szs=300x250&gdfp_req=1&ad_rule=1&output=vmap&unviewed_position_start=1&env=vp&impl=s&cmsid=496&vid=short_onecue&correlator="));
AdsMediaSource adsMediaSource = new AdsMediaSource(
mediaSource,
dataSpec,
0,
mediaSourceWithAdFactory,
adsLoader,
playerView
);
exoPlayer.setMediaSource(adsMediaSource);
exoPlayer.prepare();
exoPlayer.play();
exoPlayer.seekTo(currentPosition);
}
The main problem with this functionality is that when I previously add pre-roll
, mid-roll
, and post-roll
ads, calling exoPlayer.setMediaSource(adsMediaSource)
seems to overwrite those MediaSources
, causing them not to appear.
For example, I initially add a pre-roll
and a mid-roll
ad. The mid-roll
ad is supposed to play at the 20th second. However, if I call the method showAd()
to show a new ad at the 15th second, the mid-roll
ad at the 20th second does not appear.
EDIT-2
I recently discovered the object ConcatenatingMediaSource2.Builder()
while working on my code. It allows me to store the timeline
with both videos and ads. As a result, I can now show my ad seamlessly followed by any mid-roll
ad that had the default video.
exoPlayer.setMediaSource(mediaSourceWithAdFactory.createMediaSource(principalMediaItem));
concatenatingMediaSource = new ConcatenatingMediaSource2.Builder();
concatenatingMediaSource.setMediaSourceFactory(mediaSourceWithAdFactory);
concatenatingMediaSource.setMediaItem(principalMediaItem);
Now the showAd() method looks like this:
private void showAd(String adURL,int i) {
MediaItem mediaItem = new MediaItem.Builder()
.setUri(Uri.parse(url))
.build();
MediaSource mediaSource = mediaSourceWithAdFactory.createMediaSource(mediaItem);
DataSpec dataSpec = new DataSpec(Uri.parse(adURL));
AdsMediaSource adsMediaSource = new AdsMediaSource(
mediaSource,
dataSpec,
i,
mediaSourceWithAdFactory,
adsLoader,
playerView
);
MediaItem additionalAdItem = new MediaItem.Builder()
.setUri(Uri.parse(adURL))
.build();
concatenatingMediaSource.add(additionalAdItem, 0);
exoPlayer.setMediaSource(adsMediaSource,false);
exoPlayer.addMediaSource(concatenatingMediaSource.build());
}
I’m currently facing an issue with the showAd()
method, where I’ve encountered two specific problems:
- Mid-Roll Ad Issue:
- When attempting to play an ad before the mid-roll within a video, neither the mid-roll ad nor the post-roll appears.
- Instead, a brief blank screen is shown.
- Once the video ends, it restarts and skips the pre-roll, but the mid-roll and post-roll do play.
- No ads are displayed at their intended times.
I suspect this issue is caused by the ad I’ve inserted taking precedence in the video’s timeline, which prevents it from reverting to the original sequence.
The second issue I’m facing is related to the timing of ads:
- Only one ad is shown:
- While ads are supposed to play at specific intervals, only the first ad plays successfully.
- Subsequent ads try to load, but they remain stuck in a brief load state and are not displayed.
- This issue interrupts the ad sequence and prevents more ads from showing.
- The original pre-roll, mid-roll, and post-roll ads appear without issue.