## Play VOD streams registered with Google Cloud Video Stitcher API


This guide demonstrates how to use the IMA DAI SDK for
Android to request and play a

[Google Cloud VOD stream
session](https://cloud.google.com/video-stitcher/docs/how-to/gam/vod/overview).


This guide expands on the basic example from the [Get started
guide](https://developers.google.com/interactive-media-ads/docs/sdks/android/dai)
for IMA DAI.

For information on integrating with other platforms or on using the IMA
client-side SDKs, see [Interactive Media Ads SDKs](https://developers.google.com/interactive-media-ads).

If you would like to view or follow along with a completed sample integration,
download the
[Cloud video stitcher example](https://github.com/googleads/googleads-ima-android-dai/tree/main/CloudVideoStitcherExample).
**Note:** The IMA DAI SDK for Android supports both HLS and DASH streams.


This guide explains how to configure exoplayer and IMA DAI SDK to play an HLS stream. For an
example of a video player supporting both HLS and DASH streams, see our
[Advanced Example](https://github.com/googleads/googleads-ima-android-dai/blob/main/AdvancedExample/app/src/main/java/com/google/ads/interactivemedia/v3/samples/samplevideoplayer/SampleVideoPlayer.java#L151-L166).

## Set up a Google Cloud project


Enter the following variables for use in the IMA SDK:

Location
:   The [Google Cloud region](https://cloud.google.com/video-stitcher/docs/locations)
    where your VOD config was created:
    `LOCATION`

Project number
:   The Google Cloud project number using the Video Stitcher API:
    `PROJECT_NUMBER`

OAuth token

:   A service account's short lived OAuth token with the Video Stitcher user
    role:

    ```
    OAUTH_TOKEN
    ```

    <br />

:   Read more about

    [creating short-lived OAuth
    tokens](https://cloud.google.com/video-stitcher/docs/how-to/gam/vod/prepare-client-app-gam#generate-oauth-tokens).

    The OAuth token can be reused across multiple requests as long as it has not
    expired.

Network code

:   The Ad Manager network code for requesting ads:
    `NETWORK_CODE`

<br />

VOD config ID

:   The VOD config ID for the VOD stream:
    `VOD_CONFIG_ID`

    Read more about creating the VOD config ID in the [Cloud stitching create a
    VOD config guide](https://cloud.google.com/video-stitcher/docs/how-to/gam/vod/create-vod-configs#create-vod-config).


## Set up the basic example

Go to the [IMA Android DAI GitHub release
page](https://github.com/googleads/googleads-ima-android-dai/releases) and download
the basic example. This example is an Android app that you can open in Android
Studio for testing purposes.

> [!IMPORTANT]
> **Important:** It's important for your app to send ID3 events using [`VideoStreamPlayerCallback.onUserTextReceived()`](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/android/api/reference/com/google/ads/interactivemedia/v3/api/player/VideoStreamPlayer.VideoStreamPlayerCallback.html#public-abstract-void-onusertextreceived-string-usertext) for IMA DAI to function. See this [Full service DAI code snippet example](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/android?service=full#videoplayerappsampleadswrapperjava_2).

To test with a non-Cloud Video Stitcher VOD stream, set the `CONTENT_TYPE`
constant in SampleAdsWrapper.java to either `ContentType.VOD_HLS`, or
`ContentType.VOD_DASH`, to load the appropriate CMS ID and video ID for the
stream. Then in SampleVideoPlayer.java, set `currentlyPlayingStreamType` to
either `CONTENT_TYPE_HLS`, or `CONTENT_TYPE_DASH` ,to properly handle the
metadata of your chosen stream type.

If everything is working properly, clicking the play button on the video player
begins the short film "Tears of Steel", with a pre-roll ad break.

## Request a VOD stream

To replace the sample stream with your ad stitched VOD stream, use
[`sdkFactory.createVideoStitcherVodStreamRequest()`](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/android/api/reference/com/google/ads/interactivemedia/v3/api/ImaSdkFactory.html#public-streamrequest-createvideostitchervodstreamrequest-string-networkcode,-string-region,-string-projectnumber,-string-oauthtoken,-string-vodconfigid)
to create an ad session with Google Ad Manager. You can use the Google Ad
Manager UI to [locate the generated DAI sessions](https://support.google.com/admanager/answer/7257678) for monitoring and debugging.

`sdkFactory.createVideoStitcherVodStreamRequest()` is available on IMA DAI SDK v3.30.0 or higher.

To use [`sdkFactory.createVideoStitcherVodStreamRequest()`](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/android/api/reference/com/google/ads/interactivemedia/v3/api/ImaSdkFactory.html#public-streamrequest-createvideostitchervodstreamrequest-string-networkcode,-string-region,-string-projectnumber,-string-oauthtoken,-string-vodconfigid)
with the `vodConfigId` parameter, IMA DAI SDK version 3.33.0 or higher is required.

In the existing sample, there are conditional statements for requesting a VOD
stream or a livestream. To make it work with the Google Cloud Video Stitcher
API, you need to add a new path to return a `StreamRequest` created using
`sdkFactory.createVideoStitcherVodStreamRequest()`.

Here's an example:

**videoplayerapp/SampleAdsWrapper.java**

```java
...
private enum ContentType {
  LIVE_HLS,
  LIVE_DASH,
// Add a VOD HLS Google Cloud type. DASH streams are also supported.
VOD_HLS_GOOGLE_CLOUD,
VOD_DASH_GOOGLE_CLOUD,
  VOD_HLS,
  VOD_DASH,
}

// Set CONTENT_TYPE to the associated enum for the
// stream type you would like to test.
private static final ContentType CONTENT_TYPE =
ContentType.VOD_HLS_GOOGLE_CLOUD;
...

@Nullable
  private StreamRequest buildStreamRequest() {
    StreamRequest request;
    switch (CONTENT_TYPE) {
      ...
      case VOD_HLS_GOOGLE_CLOUD:
case VOD_DASH_GOOGLE_CLOUD:
// VOD HLS or DASH stream generated by the
// Google Cloud Video Stitcher API.
request = sdkFactory.createVideoStitcherVodStreamRequest(
"NETWORK_CODE",
"LOCATION",
"PROJECT_NUMBER",
"OAUTH_TOKEN",
"VOD_CONFIG_ID"
);
if (CONTENT_TYPE == ContentType.VOD_HLS_GOOGLE_CLOUD) {
request.setFormat(StreamFormat.HLS);
} else {
request.setFormat(StreamFormat.DASH);
}
return request;
    }
    // Content type not selected.
    return null;
  }
  ...
```

Reload the app to request and play your custom VOD stream.

### (Optional) Add streaming session options

Customize your stream request by adding session options to override the default
Cloud Video Stitcher API configuration using
[`StreamRequest.setVideoStitcherSessionOptions()`](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/android/api/reference/com/google/ads/interactivemedia/v3/api/StreamRequest.html#public-abstract-void-setvideostitchersessionoptions-mapstring,-object-videostitchersessionoptions).
If you provide an unrecognized option, the Cloud
Video Stitcher API will respond with an HTTP 400 error. Consult the
[troubleshooting guide](https://cloud.google.com/video-stitcher/docs/troubleshooting)
for assistance.

For example, you can override the
[manifest options](https://cloud.google.com/video-stitcher/docs/concepts/manifest-options)
with the following code snippet, which requests two stream manifests with
renditions ordered from lowest bitrate to highest.

```java
public void requestAndPlayAds() {
  adsLoader.addAdErrorListener(this);
  adsLoader.addAdsLoadedListener(this);
  StreamRequest streamRequest = buildStreamRequest();
// The following session options are examples. Use session options
// that are compatible with your video stream.
Map\<String, Object\> sessionOptions = Map.of(
"manifestOptions", Map.of(
"bitrateOrder", "ascending"
)
);
/\* sessionOptions JSON structure.
\* {
\* "manifestOptions": {
\* "bitrateOrder": "ascending"
\* }
\* };
\*/
streamRequest.setVideoStitcherSessionOptions(sessionOptions);
adsLoader.requestStream(streamRequest);
}
```

<br />

## Clean up

Now that you have successfully hosted a VOD stream
using the Google Cloud Video Stitcher API and requested it using the IMA DAI SDK
for Android, it's important to clean up any serving
resources.

Follow the

[VOD clean up](https://cloud.google.com/video-stitcher/docs/how-to/gam/vod/clean-up)
guide to remove any unneeded resources and assets.