AI-generated Key Takeaways
- 
          IMA SDKs facilitate integrating multimedia ads into websites and apps. 
- 
          IMA SDKs can request ads from VAST-compliant servers and manage playback. 
- 
          IMA DAI SDKs combine ad and content video into a single stream, simplifying playback management. 
- 
          IMA DAI SDKs support both VOD and live content. 
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 a- Contextand- media3.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. The IMA SDK requires library desugaring enabled, which you
must do by setting coreLibraryDesugaringEnabled true and adding
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.5' as a
dependency in the build.gradle file. For more details, see
Java 11+ APIs available through desugaring with the nio specification.
Integrate the IMA DAI SDK
- Create a new class called - SampleAdsWrapperin the- videoplayerapppackage (in- app/java/com.google.ads.interactivemedia.v3.samples/videoplayerapp/) to wrap the existing- SampleVideoPlayerand add logic implementing IMA DAI. To do this, you must first create an- AdsLoaderwhich 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_TYPEvariable.
- Create a - createSampleVideoPlayerCallback()helper method to handle creating a- SampleVideoPlayerCallbackinterface instance which extends- VideoStreamPlayer.VideoStreamPlayerCallback.- To work with DAI, the player must pass ID3 events to the IMA DAI SDK. The - callback.onUserTextReceived()method does this, in the following sample code.
- Add a - buildStreamRequest()method to create the- SteamRequest. This method switches between different streams based on how you set the- CONTENT_TYPEvariable. The default stream used in this guide is IMA's sample VOD HLS stream.
- You also need a - VideoStreamPlayerto play the stream, so add a- createVideoStreamPlayer()method, which creates an anonymous class that implements- VideoStreamPlayer.
- Implement the required listeners and add support for error handling. - Note the - AdErrorListenerimplementation, 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 - MyActivityin- videoplayerappto instantiate and call- SampleAdsWrapper. Also, make a call to- ImaSdkFactory.initialize()here using a helper method to create an- ImaSdkSettingsinstance.
- Add the - getImaSdkSettings()helper method to create an- ImaSdkSettingsinstance.
- Modify the Activity's layout file - activity_my.xmlto 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.