Define ad rules (Android)

Configure how often a viewer sees ads while watching your video content in an Android app.


After adding ads to your content, you can define ad rules that control how often a viewer sees ads while viewing your video content. Ad rules can be used only when using a VAST (VastAdvertisingConfig) ad client.

  • If you are publishing playlists that consist of short-duration videos, like tutorials or product reviews, you can define startOn (the first playlist item allowing ad playback) and frequency (the regularity of ads within a playlist).
  • If you are publishing long-form content, like webinars or interviews, you can define startOnSeek (which determines if a returning visitor is served a pre-roll ad before resuming content playback) and timeBetweenAds (which sets the minimum time that must elapse between ads playback).

You can read Ad rules reference to learn more about how each ad rule impacts a user's experience.



Implementation

Use the following steps and code samples to define ad rules:

  1. Create an AdRules object and name it, for example, adRules.
  2. Configure the properties of the adRules object for your use case.
  3. Add adRules to VastAdvertisingConfig.


Examples

Example 1: Short-form content

The following example illustrates a short-form media item with a pre-roll ad. The first ad plays before the first playlist item (startOn: 1). Then, subsequent pre-roll ads appear for every third playlist item (frequency: 3), starting with the fourth playlist item.

List<AdBreak> adSchedule = new ArrayList<>();

AdBreak adBreak = new AdBreak.Builder()
    .tag("https://www.domain.com/adtag.xml")
    .offset("pre")
    .build();

adSchedule.add(adBreak);


AdRules adRules = new AdRules.Builder()
    .startOn(1)
    .frequency(3)
    .build();
AdvertisingConfig advertising = new VastAdvertisingConfig.Builder().adRules(adRules).build();


PlaylistItem playlistItem = new PlaylistItem.Builder()
    .file("https://cdn.jwplayer.com/manifests/{media_id}.m3u8")
    .build();

List<PlaylistItem> playlist = new ArrayList<>();
playlist.add(playlistItem);


PlayerConfig config = new PlayerConfig.Builder()
    .playlist(playlist)
    .advertisingConfig(advertising)
    .autostart(true)
    .build();

getPlayer().setup(config);

Example 2: Long-form content

The following example illustrates a long-form media item with multiple ad breaks. The startOnSeek and timeBetweenAds (set to 300 seconds) ad rules have been defined.

List<AdBreak> adSchedule = new ArrayList<>();

AdBreak adBreak = new AdBreak.Builder()
    .tag("https://www.domain.com/adtag.xml")
    .offset("pre")
    .build();

AdBreak adBreak2 = new AdBreak.Builder()
    .tag("https://www.domain.com/adtag-mid-roll1.xml")
    .offset("10")
    .build();

AdBreak adBreak3 = new AdBreak.Builder()
    .tag("https://www.domain.com/adtag-mid-roll2.xml")
    .offset("00:00:15:000")
    .build();

AdBreak adBreak4 = new AdBreak.Builder()
    .tag("https://www.domain.com/adtag-mid-roll3.xml")
    .offset("25%")
    .build();

AdBreak adBreak5 = new AdBreak.Builder()
    .tag("https://www.domain.com/adtag-post-roll.xml")
    .offset("post")
    .build();

adSchedule.add(adBreak);
adSchedule.add(adBreak2);
adSchedule.add(adBreak3);
adSchedule.add(adBreak4);
adSchedule.add(adBreak5);

AdRules adRules = new AdRules.Builder()
    .startOnSeek(AdRules.RULES_START_ON_SEEK_PRE)
    .timeBetweenAds(300)
    .build();
VastAdvertisingConfig advertising = new VastAdvertisingConfig.Builder().adRules(adRules).build();


PlaylistItem playlistItem = new PlaylistItem.Builder()
    .file("https://cdn.jwplayer.com/manifests/{media_id}.m3u8")
    .build();

List<PlaylistItem> playlist = new ArrayList<>();
playlist.add(playlistItem);


PlayerConfig config = new PlayerConfig.Builder()
    .playlist(playlist)
    .advertisingConfig(advertising)
    .autostart(true)
    .build();

getPlayer().setup(config);