Preroll Ads - Android 2.4

Preroll ads are served before the content video. Use this ad format if the user is in the context of viewing content video, and you would like to show a video ad before the content video.

You can tap into Chocolate exchange demand and Google demand. You can also enable BYOD (Bring Your Own Demand) tags – in the format of VAST or VPAID – and have them compete with the aforementioned demand sources.



Mediation Partners



Enabling the Preroll Ad Unit is a Two-Stage Process.

Stage 1: Load Preroll Ad

Stage 2: Show Preroll Ad



Load Preroll Ad



1. Import Libraries

import com.vdopia.ads.lw.LVDOAdRequest; import com.vdopia.ads.lw.LVDOAdSize; import com.vdopia.ads.lw.LVDOConstants; import com.vdopia.ads.mp.MVDOAdRequest; import com.vdopia.ads.mp.PreRollVideoAd; import com.vdopia.ads.mp.PrerollAdListener;



2. Declare a PrerollVideoAd instance. In Activity or Fragment code, declare an instance variable for PrerollVideoAd

private PreRollVideoAd preRollVideoAd;



3. Initialize it.

PreRollVideoAd preRollVideoAd = PreRollVideoAd.getInstance(this);

Here, this = context



4. Set the Main Content URL and media controller for video player

preRollVideoAd.setMediaController(new MediaController(this)); preRollVideoAd.setVideoPath(contentVideo);



5. Set Ad Request Parameters & Targeting Parameters

The information you include in the targeting parameters will be used by various demand systems to display premium ads.

MVDOAdRequest adRequest = new MVDOAdRequest(this); LocationData locationData = new LocationData(this); adRequest.setLocation(locationData.getDeviceLocation()); // It is recommended that you update the below parameter - accurate info is required adRequest.setAppStoreUrl(""); adRequest.setRequester(""); adRequest.setAppDomain(""); adRequest.setAppName(""); adRequest.setCategory(""); adRequest.setPublisherDomain(""); // Passing on the following parameter values can result in better targeting and higher yield // For parameters you cannot populate, please comment them out adRequest.setDmaCode("807"); adRequest.setEthnicity("Asian"); adRequest.setPostalCode("94538"); adRequest.setCurrPostal("94539"); adRequest.setDmaCode("807"); adRequest.setAge("27"); adRequest.setMaritalStatus("single"); adRequest.setGender("m"); adRequest.setBirthday("yyyy-mm-dd");



6. Load it with an ad.

LVDOAdSize adSize = new LVDOAdSize(300, 250); preRollVideoAd.loadAd(adRequest,apiKey, adSize, AdListActivity.this, isMainContentFullscreen); Where, adRequest = Request for targeting parameters apiKey = ID for your app - Please contact Vdopia for this adSize = LVDOAdSize(adSize for preroll Ad) AdListActivity.this = PrerollAdListener isMainContentFullscreen = Boolean to show the main content in full screen or not

Please contact Vdopia to obtain your API key. You can contact us at sdk-support@vdopia.com



Show Preroll Ad



1. Import Libraries

import com.vdopia.ads.lw.LVDOAdRequest; import com.vdopia.ads.lw.LVDOConstants; import com.vdopia.ads.mp.MVDOAdRequest; import com.vdopia.ads.mp.PreRollVideoAd; import com.vdopia.ads.mp.PrerollAdListener;



2. Declare an PrerollVideoAd instance. Declare an instance variable for PrerollVideoAd in Activity or Fragment code.

private PreRollVideoAd preRollVideoAd;



3. Initialize it.

PreRollVideoAd preRollVideoAd = PreRollVideoAd.getInstance(this); preRollVideoAd.setAdContext(this); //this is Activity Object



4. Set the Main Content URL and Ad Request

preRollVideoAd.setVideoPath(contentVideo); MVDOAdRequest adRequest = new MVDOAdRequest(YourActivity.this); preRollVideoAd.setAdRequest(adRequest);



5. Set ad listener to listen various events for Preroll ad

PreRollVideoAd.setPrerollAdListener(this);



6. Add Ad to Layout

ViewGroup parent = (ViewGroup) preRollVideoAd.getParent(); if (parent != null) { parent.removeAllViews(); } mAdLayout = (RelativeLayout) findViewById(R.id.adLayout); mAdLayout.addView(preRollVideoAd);



7. Show your ad

PreRollVideoAd.showAd();



Sample code

Sample codes below are from Vdopia Sample App. The high level flow looks like below. AdListActivity → AdPartnersListActivity → MediaActivity AdListActivity - This is where we load the ad MediaActivity - This is where we show the ad



See below for code snippets in AdListActivity class.

public class AdListActivity extends RequestPermissionActivity { private void initializePrerollAd() { Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { @Override public void run() { String apiKey = “”; boolean isMainContentFullscreen = true; LVDOAdSize adSize = new LVDOAdSize(LVDOAd_WIDTH, LVDOAd_HEIGHT); preRollVideoAd = PreRollVideoAd.getInstance(AdListActivity.this); preRollVideoAd.setMediaController(new MediaController(AdListActivity.this)); MVDOAdRequest adRequest = new MVDOAdRequest(AdListActivity.this); LocationData locationData = new LocationData(AdListActivity.this); adRequest.setLocation(locationData.getDeviceLocation()); // It is recommended that you update the below parameter - accurate info is required adRequest.setAppStoreUrl(""); adRequest.setRequester(""); adRequest.setAppDomain(""); adRequest.setAppName(""); adRequest.setCategory(""); adRequest.setPublisherDomain(""); // Passing on the following parameter values can result in better targeting and higher yield // For parameters you cannot populate, please comment them out adRequest.setDmaCode("807"); adRequest.setEthnicity("Asian"); adRequest.setPostalCode("94538"); adRequest.setCurrPostal("94539"); adRequest.setDmaCode("807"); adRequest.setAge("27"); adRequest.setMaritalStatus("single"); adRequest.setGender("m"); adRequest.setBirthday("yyyy-mm-dd"); preRollVideoAd.loadAd(adRequest, apiKey, adSize, AdListActivity.this, isMainContentFullscreen); Log.v(TAG, "Activity : " + this); } } } }



See below for code snippets in MediaActivity class.

public class MediaActivity extends RequestPermissionActivity implements PrerollAdListener { private static final String TAG = "MediaActivity"; private RelativeLayout mAdLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_media); mAdLayout = (RelativeLayout) findViewById(R.id.adLayout); // Permission handling code for Android 6.0 and Above super.requestAppPermissions(mPermissions, R.string.runtime_permissions, REQUEST_PERMISSIONS); } @Override public void onPermissionsGranted(int requestCode) { showPrerollAd(); } private void showPrerollAd() { preRollVideoAd = PreRollVideoAd.getInstance(this); String contentVideo = "http://cdn.vdopia.com/files/happy.mp4"; ViewGroup parent = (ViewGroup) preRollVideoAd.getParent(); if (parent != null) { parent.removeAllViews(); } mAdLayout.addView(preRollVideoAd); preRollVideoAd.setPrerollAdListener(this); preRollVideoAd.showAd(); } @Override public void onPrerollAdLoaded(View prerollAd) { Log.d(TAG, "PreRoll Video Ad onPrerollAdLoaded"); } @Override public void onPrerollAdFailed(View prerollAd, LVDOAdRequest.LVDOErrorCode errorCode) { Log.d(TAG, "PreRoll Video Ad onPrerollAdFailed"); } @Override public void onPrerollAdClicked(View prerollAd) { Log.d(TAG, "PreRoll Video Ad onPrerollAdClicked"); } @Override public void onPrerollAdCompleted(View prerollAd) { Log.d(TAG, "PreRoll Video Ad onPrerollAdCompleted"); } @Override public void onPrepareMainContent(MediaPlayer player) { Log.d(TAG, "PreRoll Video onPrepareMainContent"); } @Override public void onErrorMainContent(MediaPlayer player, int code) { Log.d(TAG, "PreRoll Video onErrorMainContent : " + code); setContentVisibility(); } @Override public void onCompleteMainContent(MediaPlayer player) { Log.d(TAG, "PreRoll Video onCompleteMainContent"); setContentVisibility(); } private void setContentVisibility() { MediaActivity.this.finish(); } }



Event Listeners

You may optionally track ad lifecycle events like request failures or “click-through” by implementing com.vdopia.ads.mp. This interface may be implemented by your activity or any other object.

// called when an ad is loaded public void onPrerollAdLoaded(View prerollAd); // called when there is no ad Public void onPrerollAdFailed(View prerollAd,LVDOAdRequest.LVDOErrorCode errorCode ); // called when an ad is clicked public void onPrerollAdClicked(View prerollAd); // called when an ad is completed public void onPrerollAdCompleted(View prerollAd); // called when the main content is prepared and ready to play public void onPrepareMainContent(MediaPlayer player); // called when an error occurred while main content was playing public void onErrorMainContent(MediaPlayer player, int code); // called when playing of the main content is completed public void onCompleteMainContent(MediaPlayer player);

Let’s get started! Here are steps for integration

1. Add SDK to Android App as Compiled .aar (Gradle project)

To add the Vdopia SDK as an .aar in your project, the SDK from download section, and then copy the .aar to your application library directory.