Video Ads Integration for Android
Do go through the steps in our Getting Started Guide to add Media.Net SDK into your project before starting the integration of Video Ads.
For adding video ads to your app, following are the steps:
- Add the video dependency to your build.gradle file
- Define an ad slot
- Load an ad in the ad slot
-
Set up listeners
a. Add Video dependency to your build.gradle file
For video ads, it is required to add an additional dependency to your build.gradle file. The below-mentioned dependencies need to be added to your build.gradle.
dependencies { compile "net.media.android:base:1.2.5" compile "net.media.android:video:1.2.5" }
b. Define a slot for your Video ad in your layout XML.
The Media.net SDK provides a custom View subclass, MNetView, which handles requesting and loading ads.
Include this XML block to your activity’s or fragment’s layout.
<net.media.android.base.view.MNetView android:id="@+id/mnet_view" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
c. Load an ad into the video slot
MNetView mnetView = (MNetView)findViewById(R.id.mnet_view); mnetView.setAdUnitId("xxxxxxxxxxx");//Enter your Ad Unit ID mentView.loadAd();
d. Setting up listeners
- Listen to the Ad Lifecycle Events
MNetView provides a listener interface, AdListener, which can be used to stay informed about the ad lifecycle events. AdListener has the following methods.
mnetView.addListener(new AdListener() { @Override // Sent when the ad slot successfully retrieved a Video ad. public void onAdLoaded() { //Do your stuff } @Override // Sent when the ad slot has failed to retrieve an ad. public void onError(final MNetError error) { //Do your stuff } @Override // Sent when the user has tapped on the Video ad. public void onAdClicked() { //Do your stuff } @Override // Sent when the user closes the Ad public void onAdClosed() { //Do your stuff } });
- Listen to the Video Lifecycle Events
MNetView provides a listener interface, VideoAdListener, which can be used to stay informed about the lifecycle events of Video Ads. VideoAdListener has the following methods.
mMNetView.addVideoAdListener(new VideoAdListener() { @Override public void onVideoStarted() { // Do your stuff } @Override public void onVideoCompleted() { // Do your stuff } });