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.
This guide demonstrates how to play a DAI Pod Serving stream, using the IMA Android DAI SDK with a simple video player for stream playback. If you would like to view or follow along with a completed sample integration, download the pod serving example.
IMA DAI Pod Serving overview
-
StreamRequest
: An object that defines a stream request to Google's advertising servers. Must be created usingImaSdkFactory.createPodStreamRequest()
to enable pod serving. This method specifies a Network Code, Custom Asset Key, and an optional API key. -
StreamManager
: An object that handles communication between the video stream and the IMA DAI SDK, such as firing tracking pings and forwarding stream events to the publisher.
Prerequisites
Before you begin, you need the following:
- An Android app already set up with the IMA DAI SDK to play video streams with DAI ads. If you do not already have such an app we recommend using the Android DAI BasicExample as a starting point. The BasicExample will be the codebase referenced in this guide.
1. Set up your pod serving variables
All changes needed for pod serving will be done in SampleAdsWraper.java. The first step will be to update the constant variables.
Here are the ad pod stream request constants this guide will be adding:
STREAM_URL
: The video stream url provided by your manifest manipulator or 3rd party partner using Pod Serving. It should require you to insert the stream ID provided by the IMA DAI SDK, before you make a request. In this case, the stream url includes a placeholder, [[STREAMID]], which will be replaced with the stream ID, before making a request.NETWORK_CODE
: The network code for your AdManager360 account.CUSTOM_ASSET_KEY
: The custom asset key that identifies your pod serving event in AdManager360. This may be created by your manifest manipulator or 3rd party pod serving partner.API_KEY
: An optional API key that can be required to retrieve a Stream ID from the IMA DAI SDK.
The Android DAI BasicExample is designed to play a variety of different stream types, but for pod serving it will be set up to just play a single stream. Change the example's variable section to match the following:
... /** This class adds ad-serving support to Sample HlsVideoPlayer */ public class SampleAdsWrapper implements AdEvent.AdEventListener, AdErrorEvent.AdErrorListener, AdsLoader.AdsLoadedListener { // Podserving Stream Constants. private static final String STREAM_URL = "https://encodersim.sandbox.google.com/masterPlaylist/9c654d63-5373-4673-8c8d-6d92b66b9d46/" + "master.m3u8?gen-seg-redirect=true&network=51636543&event=google-sample" + "&pids=devrel4628000,devrel896000,devrel3528000,devrel1428000,devrel2628000,devrel1928000" + "&seg-host=dai.google.com&stream_id=[[STREAMID]]"; private static final String NETWORK_CODE = "51636543"; private static final String CUSTOM_ASSET_KEY = "google-sample"; private static final String API_KEY = ""; private String streamUrl; private static final String PLAYER_TYPE = "DAISamplePlayer"; /** Log interface, so we can output the log commands to the UI or similar. */ public interface Logger { ...
2. Call the ImaSdkFactory.createPodStreamRequest() to enable pod serving
Remove the method buildStreamRequest()
which had been used to switch between
building a variety of stream types. Then, change requestAndPlayAds()
to call
sdkFactory.createPodStreamRequest()
to create a pod serving ad request.
Finally, request the stream using AdsLoader.requestStream()
.
public void requestAndPlayAds() { StreamRequest request = sdkFactory.createPodStreamRequest(NETWORK_CODE, CUSTOM_ASSET_KEY, API_KEY); request.setFormat(StreamFormat.HLS); adsLoader.addAdErrorListener(this); adsLoader.addAdsLoadedListener(this); adsLoader.requestStream(request); }
3 Edit and set the stream URL
Call StreamManager.getStreamId()
to get the stream ID. This will then need to
be inserted into the STEAM_URL
replacing "[[STREAMID]]"
. Once this change
has been made, the new stream URL can be set using videoPlayer.setStreamUrl()
.
@Override public void onAdsManagerLoaded(AdsManagerLoadedEvent event) { streamManager = event.getStreamManager(); streamManager.addAdErrorListener(this); streamManager.addAdEventListener(this); // To enable streams String streamID = streamManager.getStreamId(); streamUrl = STREAM_URL.replace("[[STREAMID]]", streamID); streamManager.init(); videoPlayer.setStreamUrl(streamUrl); videoPlayer.play(); }
When pod serving is enabled, IMA will not make a call to
VideoStreamPlayer.loadUrl()
, so feel free to remove the calls to
videoPlayer.setStreamUrl()
and videoPlayer.play()
found there.
That's it! You're now requesting and displaying ads in a pod serving stream with the IMA Android DAI SDK. To see other examples of the Android SDK being used, look to the samples on GitHub.