Get started

IMA SDKs make it easy to integrate multimedia ads into your websites and apps. IMA SDKs can request ads from any VAST-compliant ad server and manage ad playback in your apps. With IMA client-side SDKs, you maintain control of content video playback, while the SDK handles ad playback. Ads play in a separate video player positioned on top of the app's content video player.

This guide demonstrates how to integrate the IMA SDK into an empty Android Studio project using the ExoPlayer IMA extension. If you would like to view or follow along with a completed sample integration, download the BasicExample from GitHub.

IMA client-side overview

Implementing IMA client-side involves four main SDK components, which are demonstrated in this guide:

  • AdDisplayContainer: A container object where ads are rendered.
  • AdsLoader: An object that requests ads and handles events from ads request responses. You should only instantiate one ads loader, which can be reused throughout the life of the application.
  • AdsRequest: An object that defines an ads request. Ads requests specify the URL for the VAST ad tag, as well as additional parameters, such as ad dimensions.
  • AdsManager: An object that contains the response to the ads request, controls ad playback, and listens for ad events fired by the SDK.

Prerequisites

Before you begin, you need Android Studio 3.0 or higher.

1. Create a new Android Studio project

To create your Android Studio project, complete the following steps:

  1. Start Android Studio.
  2. Select Start a new Android Studio project.
  3. In the Choose your project page, select the Empty Activity template.
  4. Click Next.
  5. In the Configure your project page, name your project and select Java for the language.
  6. Click Finish.

2. Add the ExoPlayer IMA extension to your project

First, in the application-level build.gradle file, add imports for the extension to the dependencies section. Also, add new compileOptions to specify the Java version compatibilty information.

app/build.gradle
android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    ...
}
dependencies {
    implementation 'com.google.android.exoplayer:exoplayer-core:2.11.1'
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.11.1'
    implementation 'com.google.android.exoplayer:extension-ima:2.11.1'

    ...
}

Add the user permissions required by the IMA SDK for requesting ads.

app/src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.project name">

    <!-- Required permissions for the IMA SDK -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    ...

</manifest>

3. Create the ad UI container

Create the view to use as the ExoPlayer PlayerView by creating a RelativeLayout object with an appropriate ID. Also change the androidx.constraintlayout.widget.ConstraintLayout to a LinearLayout.

app/src/main/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

4. Add your content URL and ad tag URL for the ads request

Add entries to strings.xml to store your content URL and VAST ad tag URL.

app/src/main/res/values/strings.xml
<resources>
    <string name="app_name">Your_Project_Name</string>
    <string name="content_url"><![CDATA[https://storage.googleapis.com/gvabox/media/samples/stock.mp4]]></string>
    <string name="ad_tag_url"><![CDATA[https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ct%3Dlinear&correlator=]]></string>

</resources>

5. Import the ExoPlayer IMA extension

Add the import statements for the ExoPlayer extension. Then, update the MainActivity class to extend Activity by adding private variables for the PlayerView, SimpleExoPlayer, and ImaAdsLoader.

app/src/main/java/com/example/project name/MainActivity.java

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ext.ima.ImaAdsLoader;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.source.ads.AdsMediaSource;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;

...

public class MainActivity extends Activity {

  private PlayerView playerView;
  private SimpleExoPlayer player;
  private ImaAdsLoader adsLoader;

}

6. Create an adsLoader instance

Overwrite the onCreate method and add the required variable assignments to create a new adsLoader object with the ad tag URL.

app/src/main/java/com/example/project name/MainActivity.java

...

public class MainActivity extends Activity {

  private PlayerView playerView;
  private SimpleExoPlayer player;
  private ImaAdsLoader adsLoader;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    playerView = findViewById(R.id.player_view);

    // Create an AdsLoader with the ad tag url.
    adsLoader = new ImaAdsLoader(this, Uri.parse(getString(R.string.ad_tag_url)));
  }

}

7. Initialize and release the player

Add methods to initialize and release the player. In the initialize method, create the SimpleExoPlayer. Then, create the AdsMediaSource and set it to the player.

app/src/main/java/com/example/project name/MainActivity.java
public class MainActivity extends Activity {

  ...

  private void releasePlayer() {
    adsLoader.setPlayer(null);
    playerView.setPlayer(null);
    player.release();
    player = null;
  }

  private void initializePlayer() {
    // Create a SimpleExoPlayer and set is as the player for content and ads.
    player = new SimpleExoPlayer.Builder(this).build();
    playerView.setPlayer(player);
    adsLoader.setPlayer(player);

    DataSource.Factory dataSourceFactory =
            new DefaultDataSourceFactory(this, Util.getUserAgent(this, getString(R.string.app_name)));

    ProgressiveMediaSource.Factory mediaSourceFactory =
            new ProgressiveMediaSource.Factory(dataSourceFactory);

    // Create the MediaSource for the content you wish to play.
    MediaSource mediaSource =
            mediaSourceFactory.createMediaSource(Uri.parse(getString(R.string.content_url)));

    // Create the AdsMediaSource using the AdsLoader and the MediaSource.
    AdsMediaSource adsMediaSource =
            new AdsMediaSource(mediaSource, dataSourceFactory, adsLoader, playerView);

    // Prepare the content and ad to be played with the SimpleExoPlayer.
    player.prepare(adsMediaSource);

    // Set PlayWhenReady. If true, content and ads autoplay.
    player.setPlayWhenReady(false);
  }
}

8. Handle player events

Finally, create callbacks for the player's lifecycle events:

  • onStart
  • onResume
  • onStop
  • onPause
  • onDestroy
app/src/main/java/com/example/project name/MainActivity.java
public class MainActivity extends Activity {

  private PlayerView playerView;
  private SimpleExoPlayer player;
  private ImaAdsLoader adsLoader;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_my);

      playerView = findViewById(R.id.player_view);

      // Create an AdsLoader with the ad tag url.
      adsLoader = new ImaAdsLoader(this, Uri.parse(getString(R.string.ad_tag_url)));
  }

  @Override
  public void onStart() {
    super.onStart();
    //
    if (Util.SDK_INT > 23) {
      initializePlayer();
      if (playerView != null) {
        playerView.onResume();
      }
    }
  }

  @Override
  public void onResume() {
    super.onResume();
    if (Util.SDK_INT <= 23 || player == null) {
      initializePlayer();
      if (playerView != null) {
        playerView.onResume();
      }
    }
  }

  @Override
  public void onPause() {
    super.onPause();
    if (Util.SDK_INT <= 23) {
      if (playerView != null) {
        playerView.onPause();
      }
      releasePlayer();
    }
  }

  @Override
  public void onStop() {
    super.onStop();
    if (Util.SDK_INT > 23) {
      if (playerView != null) {
        playerView.onPause();
      }
      releasePlayer();
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    adsLoader.release();
  }

  ...

}

That's it! You're now requesting and displaying ads with the IMA SDK. To learn about additional SDK features, see the other guides or the samples on GitHub.