How to show ads at specific time using ExoPlayer with IMA Integrated?

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật