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 DAI SDKs, apps make a stream request for ad and content video—either VOD or live content. The SDK then returns a combined video stream, so that you don't have to manage switching between ad and content video within your app.
Select the DAI solution you're interested in
Full service DAI
This guide demonstrates how to integrate the IMA DAI SDK into a simple video player app. If you would like to view or follow along with a completed sample integration, download the BasicExample from GitHub.
IMA DAI overview
Implementing IMA DAI involves four main SDK components as demonstrated in this guide:
StreamDisplayContainer
: A container object that sits on top of the video playback element and houses the ad UI elements.AdsLoader
: An object that requests streams and handles events triggered by stream request response objects. You should only instantiate one ads loader, which can be reused throughout the life of the application.StreamRequest
: An object that defines a stream request. Stream requests can either be for video-on-demand or live streams. Live stream requests specify an asset key, while VOD requests specify a CMS ID and video ID. Both request types can optionally include an API key needed to access specified streams, and a Google Ad Manager network code for the IMA SDK to handle ads identifiers as specified in Google Ad Manager settings.StreamManager
: An object that handles dynamic ad insertion streams and interactions with the DAI backend. The stream manager also handles tracking pings and forwards stream and ad events to the publisher.
Prerequisites
- Android Studio
- Sample video player app for the SDK integration
Download and run the sample video player app
The sample app provides a working video player that plays HLS video. Use this as a starting point for integrating the IMA DAI SDK's DAI capabilities.
Download the sample video player app and extract it.
Start Android Studio and select Open an existing Android Studio project, or if Android Studio is already running, select File > New > Import Project. Then choose
SampleVideoPlayer/build.gradle
.Run a Gradle sync by selecting Tools > Android > Sync Project with Gradle Files.
Ensure that the player app compiles and runs on a physical Android device or an Android Virtual Device using Run > Run 'app'. It's normal for the video stream to take a few moments to load before playing.
Examine the sample video player
The sample video player doesn't contain any IMA DAI SDK integration code yet. The sample app consists of two major parts:
samplevideoplayer/SampleVideoPlayer.java
: An ExoPlayer-based HLS player that serves as the basis for IMA DAI integration.videoplayerapp/MyActivity.java
: This activity creates the video player and passes it aContext
andmedia3.ui.PlayerView
.
Add the IMA DAI SDK to the player app
You must also include a reference to the IMA DAI SDK. In Android Studio, add the
following to your application-level build.gradle
file, located at
app/build.gradle
:
Integrate the IMA DAI SDK
Create a new class called
SampleAdsWrapper
in thevideoplayerapp
package (inapp/java/com.google.ads.interactivemedia.v3.samples/videoplayerapp/
) to wrap the existingSampleVideoPlayer
and add logic implementing IMA DAI. To do this, you must first create anAdsLoader
which is used to request the DAI stream.This snippet includes sample parameters for HLS and DASH, live and VOD streams. To set which stream is being played, update the
CONTENT_TYPE
variable.Create a
createSampleVideoPlayerCallback()
helper method to handle creating aSampleVideoPlayerCallback
interface instance which extendsVideoStreamPlayer.VideoStreamPlayerCallback
.To work with DAI, the player must pass ID3 events for livestreams to the IMA DAI SDK. The
callback.onUserTextReceived()
method does this, in the following sample code.Add a
buildStreamRequest()
method to create theSteamRequest
. This method switches between different streams based on how you set theCONTENT_TYPE
variable. The default stream used in this guide is IMA's sample VOD HLS stream.You also need a
VideoStreamPlayer
to play the stream, so add acreateVideoStreamPlayer()
method, which creates an anonymous class that implementsVideoStreamPlayer
.Implement the required listeners and add support for error handling.
Note the
AdErrorListener
implementation, as it calls a fallback URL if the ads fail to play. Since the content and ads are in one stream, you must be ready to call a fallback stream if the DAI stream encounters an error.Add in code for logging.
Modify
MyActivity
invideoplayerapp
to instantiate and callSampleAdsWrapper
. Also, make a call toImaSdkFactory.initialize()
here using a helper method to create anImaSdkSettings
instance.Add the
getImaSdkSettings()
helper method to create anImaSdkSettings
instance.Modify the Activity's layout file
activity_my.xml
to add UI elements for logging.
Congrats! You're now requesting and displaying video ads in your Android app. To fine tune your implementation, see Bookmarks, Snapback, and the API documentation.
Troubleshooting
If you're experiencing issues playing a video ad, try downloading the completed BasicExample. If it works properly in the BasicExample then there's likely an issue with your app's IMA integration code.
If you're still having issues, visit the IMA SDK forum.