AMP Android SDK: IMA Integration

Document created by Jason Gamboa Employee on Jan 3, 2017Last modified by Herberth Alvarado on Apr 9, 2017
Version 5Show Document
  • View in full screen mode

Prerequisite

The rest of this guide assumes you have successfully integrated AMP's Core (you are able to play back a video). 

 

Getting started

For reference, check the IMA Sample Android Studio sample project in the release package.
To integrate the plugin into your app, you need to:

 

1) Add the amp-ads-google-ima.jar and ima-android-sdk-beta13_3.jar to your project's /libs folder.
 

2) Add the following line in the runnable module build.gradle file:

compile 'com.google.android.gms:play-services-ads:9.6.1'

3) In the Activity where playback is handled, import the following Java packages:

import com.akamai.ads.AdsHelper;
import com.akamai.ads.IAdsComponentListener;
import com.akamai.ima.AdsComponent;

 

4) Add an object of the AdsComponent type in your Activity's members:

private AdsComponent adsComponent;

 

5) Initialize that object, on the onCreate() method from the Activity's life cycle:

 

 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   .
   .
     adsComponent = new AdsComponent(PlayerActivity.this);
}

 

6) Make your Activity implement the IAdsComponentListener. This interface gives access to a set of callbacks linked to the ads behavior:

   

@Override
public void onAdsError(String s) {
}

@Override
public void onPauseContentRequested() {
}

@Override
public void onResumeContentRequested() {
}

@Override
public void onAdsLoaded() {
}

@Override
public void onAdsStarted() {
}

@Override
public void onAdsPaused() {
}

@Override
public void onAdsResumed() {
}

@Override
public void onAdsEnded() {
}

@Override
public void onAdsTrackProgress(int i) {
}

@Override
public void onAdsPlayheadUpdate(int i) {
}

 

7) Configure the AdsComponent object when the onResourceReady(MediaResource resource) callback is triggered as follows:

 

@Override
public void onResourceReady(MediaResource resource) {
   .
   .
adsComponent.setVideoPlayerView(mVideoView);
adsComponent.attachToVideoPlayerContainer(mVideoContainer);
adsComponent.addEventsListener(this);
adsComponent.setAdsUrl(adsURL);
.
.
}

Sidenote: The IMA URL can be provided manually as the sample demonstrates, however, if you are using the Configuration files model AMP provides for all its players, you can obtain the data needed for the Ads from there, in that case, the last line is not necessary. For more details, please review the Configuration Files guideline.

 

8) Add the following line to the onDestroy() method of the Activity's life cycle:

@Override
public void onDestroy(){
super.onDestroy();
adsComponent.onDestroy();
   }

Optional:

 

You can add the following implementation to the onResume() and onPause() methods of the Activity's life cycle, yo implement automatic pause and recovery of the Ads in case the App implementing the Ads loses its foreground status.

 

@Override
public void onPause() {
    if(adsComponent!=null)
      adsComponent.pauseAd();
    super.onPause();
}

@Override
public void onResume() {
    if(adsComponent!=null)
      adsComponent.resumeAd();
    super.onResume();
}
1 person found this helpful

Attachments

    Outcomes