Schedule Google IMA ads (Android v3)
Add advertising breaks to your content when using the Google IMA ad client in an Android app.
After adding the Google IMA SDK to your app and acquiring the required items listed in the Requirements section, you can schedule Google IMA ads in your Android app.
Requirements
Add a pre-roll ad to a playlist
If you use the IAB Open Measurement Interface Definition (OMID) and run Google IMA ads, all custom video controls that overlay the media element must be registered as friendly obstructions.
Use the following steps to add a pre-roll ad to the player you added to your activity:
- In app/java/MainActivity.java, create a
List<AdBreak>object and name it, for example,adSchedule. - Create an
AdBreakobject and name it, for example,adBreak. At the minimum, you must pass an ad tag to thetagproperty. We strongly recommend using a secure ad tag URL. - Add
adBreaktoadSchedule. - Create an
ImaAdvertisingobject and name it, for example,imaAdvertising. UseadSchedule(which defines the ad schedule to use) as the argument. - Add
advertising(imaAdvertising)to theconfigobject of the player. This adds the ad schedule to the player.
mPlayerView = findViewById(R.id.jwplayer);
List<AdBreak> adSchedule = new ArrayList<>();
AdBreak adBreak = new AdBreak.Builder()
.tag("https://www.domain.com/adtag.xml")
.offset("pre")
.build();
adSchedule.add(adBreak);
ImaAdvertising imaAdvertising = new ImaAdvertising(adSchedule);
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)
.advertising(imaAdvertising)
.build();
mPlayerView.setup(config);
You can build upon this basic implementation by adding multiple ad breaks.
Add multiple ad breaks to a player
If you use the IAB Open Measurement Interface Definition (OMID) and run Google IMA ads, all custom video controls that overlay the media element must be registered as friendly obstructions.
Use the following steps to add multiple ad breaks to the previous VAST pre-roll example:
- Define an additional
AdBreakobject. - Pass an ad tag to the
tagproperty. - When defining the
offsetproperty, choose one of the following values to schedule a mid-roll or post-roll ad.
Mid-roll
  - {number}: (String) Ad plays after the specified number of seconds.
  - {timecode}: (String) Ad plays at a specific time, inhh:mm:ss:mmmformat.
Post-roll
  -post: (String) Ad plays after the content. - Add the additional
AdBreakobject toadSchedule.
mPlayerView = findViewById(R.id.jwplayer);
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);
ImaAdvertising imaAdvertising = new ImaAdvertising(adSchedule);
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)
.advertising(imaAdvertising)
.build();
mPlayerView.setup(config);
Updated almost 3 years ago
