1. Home
  2. Publishers
  3. Integrating Pre-Roll video ads

Integrating Pre-Roll video ads

Step 1: Setup SDK

Setup Tapsell SDK with instructions provided on this page:

Step 2: Add the Google IMA Library

In Tapsell SDK, to play pre-roll ads in different players, we have used the google IMA library. Insert the following code in your app/build.gradle file:

compile 'com.google.ads.interactivemedia.v3:interactivemedia:3.8.7'
compile 'com.google.android.gms:play-services-ads:15.0.1'

Step 3: Showing ad

For starters create an instance from the TapsellVAST class.

TapsellVast tapsellVast = new TapsellVast(context, "fa");

The constructor takes context and a string, indicating desired language for ad’s text as arguments.
Before playing the main video, request an ad with the following code:

tapsellVast.requestAds(videoAdPlayer, adUiContainer, adTagUrl, contentProgressProvider);
  • videoAdPlayer : ir.tapsell.sdk.vast.VideoAdPlayer, an interface for controlling the video player. Step 5 explains how to create one.
  • adUiContainer : an instance of view group, in which your player is located.
  • adTagUrl : an String. Step 4 explains how to get this string.
  • contentProgressProvider :ir.tapsell.sdk.vast.ContentProgressProvider, an interface for video’s progress. Step 6 explains how the create one.

Step 4: Getting the AdTagUrl

You can get the String using the following function:

String vastUrl = TapsellVast.getVastUrl(context, zoneId, prerollType, vastVersion);

zoneId is the identifier of the zone you want to get ads for, you can find it in your panel. For the default zone, you can also use null. prerollType and vastVersion are integers. You can find their accepted values in the tables below.

Table1-Accepted values for prerollType
Value Description
PREROLL_TYPE_SHORT Show a short video ad (5sec)
PREROLL_TYPE_LONG Show a long video ad (30sec)
Table2-Accepted values for vastVersion
Value Description
VAST_VERSION_2 Use VAST version 2
VAST_VERSION_3 Use VAST version 3

Step 5: Creating VideoAdPlayer Object

VideoAdPlayer is an interface for controlling video ad playback. Your player should implement this interface’s methods  and perform required actions. The methods are listed below.

Table3-VastVideoPlayer methods to be implemented
Method Description
()void playAd Start playing the ad loaded in loadAd()
(String url)void loadAd Load video ad from url
()void stopAd Stop playing current ad
()void pauseAd Pause ad playback
()void resumeAd Resume ad playback
void addCallback(VideoAdPlayerCallback callback) Add a callback given to you (you should call these callbacks)
void removeCallback(VideoAdPlayerCallback callback) Remove a callback given to you in addCallback
VideoProgressUpdate getAdProgress() Return video progress as a VideoAdProgressUpdate object

Step 6: Creating ContentProgressProvider Object

ContentProgressProvider is an interface that your player should implement its methods and perform required actions. The methods are listed below.

Table4-ContentProgressProvider method to be implemented
Method Description
VideoProgressUpdate getContentProgress() Return video progress and video lenght as a VideoAdProgressUpdate object

Step 7: Adding Callbacks

You can use TapsellVastAdsListner for monitoring events. You can use setTapsellVastAdsListner method like the code below:

tapsellVast.setTapsellVastAdsListener(new TapsellVastAdsListener() {
    @Override
    public void onAdError(String message) {
        // An error occurred on ads request 
    }

    @Override
    public void onAdEvent(AdEvent adEvent) {
       
        switch (adEvent) {
            case CONTENT_PAUSE_REQUESTED:
                // CONTENT_PAUSE_REQUESTED is fired immediately before
                // a video ad is played.
                break;
            case CONTENT_RESUME_REQUESTED:
                // CONTENT_RESUME_REQUESTED is fired when the ad is
                // completed and you should start playing your content.
                break;
            case PAUSED:
                // Fired when ad is paused
                break;
            case RESUMED:
                // Fired when ad is resumed
                break;
            default:
                break;
        }
    }
});

Step 8: Additional Methods

  • When you main content is done playing, use this method:
tapsellVast.contentComplete();
  • If for any reason your video player paused playback (e.g. the activity’s onPause and onResume), please use the following methods:
// call after pausing video player when playing ad
tapsellVast.pause();
// call after resuming player to play ad
tapsellVast.resume();

Final Step : Proguard rules

Make sure that you include Tapsell’s proguard rules in your own rules file.

Sample Project

A sample project is provided in the following repository in case more information about the SDK is needed.

 

 

 

Please let us know whether this document was helpful and what complications you faced while using Tapsell Android SDK.

 

 

Next: Integrating Native Video ads in your app
Prev: Integrating Interstitial/Rewarded Video and Interstitial Banner Ads in Android Project

 

Was this article helpful?

Related Articles