在现有示例中,有用于请求 VOD 流式传输或直播的条件语句。为了使其与 Google Cloud Video Stitcher API 配合使用,您需要添加一个新路径,以返回使用 sdkFactory.createVideoStitcherVodStreamRequest() 创建的 StreamRequest。
示例如下:
videoplayerapp/SampleAdsWrapper.java
...privateenumContentType{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.privatestaticfinalContentTypeCONTENT_TYPE=ContentType.VOD_HLS_GOOGLE_CLOUD;...@NullableprivateStreamRequestbuildStreamRequest(){StreamRequestrequest;switch(CONTENT_TYPE){...caseVOD_HLS_GOOGLE_CLOUD:caseVOD_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);}returnrequest;}// Content type not selected.returnnull;}...
publicvoidrequestAndPlayAds(){adsLoader.addAdErrorListener(this);adsLoader.addAdsLoadedListener(this);StreamRequeststreamRequest=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);}
清理
现在,您已成功使用 Google Cloud Video Stitcher API 托管 VOD 流式传输,并使用 Android 版 IMA DAI SDK 请求了该流式传输,因此请务必清理所有广告投放资源。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[],[],null,["# Use the IMA DAI SDK on Android\n\nPlay VOD streams registered with Google Cloud Video Stitcher API\n----------------------------------------------------------------\n\n\nThis guide demonstrates how to use the IMA DAI SDK for\nAndroid to request and play a\n\n[Google Cloud VOD stream\nsession](//cloud.google.com/video-stitcher/docs/how-to/gam/vod/overview).\n\n\nThis guide expands on the basic example from the [Get started\nguide](//developers.google.com/interactive-media-ads/docs/sdks/android/dai)\nfor IMA DAI.\n\nFor information on integrating with other platforms or on using the IMA\nclient-side SDKs, see [Interactive Media Ads SDKs](//developers.google.com/interactive-media-ads).\n\nIf you would like to view or follow along with a completed sample integration,\ndownload the\n[Cloud video stitcher example](//github.com/googleads/googleads-ima-android-dai/tree/main/CloudVideoStitcherExample).\n| **Note:** The IMA DAI SDK for Android supports both HLS and DASH streams.\n|\n|\n| This guide explains how to configure exoplayer and IMA DAI SDK to play an HLS stream. For an\n| example of a video player supporting both HLS and DASH streams, see our\n| [Advanced Example](//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).\n\nSet up a Google Cloud project\n-----------------------------\n\n\nEnter the following variables for use in the IMA SDK:\n\nLocation\n: The [Google Cloud region](//cloud.google.com/video-stitcher/docs/locations)\n where your VOD config was created:\n \u003cvar scope=\"LOCATION\" class=\"edit\" translate=\"no\"\u003eLOCATION\u003c/var\u003e\n\nProject number\n: The Google Cloud project number using the Video Stitcher API:\n \u003cvar scope=\"PROJECT_NUMBER\" class=\"edit\" translate=\"no\"\u003ePROJECT_NUMBER\u003c/var\u003e\n\nOAuth token\n\n: A service account's short lived OAuth token with the Video Stitcher user\n role:\n\n ```\n OAUTH_TOKEN\n ```\n\n \u003cbr /\u003e\n\n: Read more about\n\n [creating short-lived OAuth\n tokens](//cloud.google.com/video-stitcher/docs/how-to/gam/vod/prepare-client-app-gam#generate-oauth-tokens).\n\n The OAuth token can be reused across multiple requests as long as it has not\n expired.\n\nNetwork code\n\n: The Ad Manager network code for requesting ads:\n \u003cvar scope=\"NETWORK_CODE\" class=\"edit\" translate=\"no\"\u003eNETWORK_CODE\u003c/var\u003e\n\n\u003cbr /\u003e\n\nVOD config ID\n\n: The VOD config ID for the VOD stream:\n \u003cvar scope=\"VOD_CONFIG_ID\" class=\"edit\" translate=\"no\"\u003eVOD_CONFIG_ID\u003c/var\u003e\n\n Read more about creating the VOD config ID in the [Cloud stitching create a\n VOD config guide](//cloud.google.com/video-stitcher/docs/how-to/gam/vod/create-vod-configs#create-vod-config).\n\n\nSet up the basic example\n------------------------\n\nGo to the [IMA Android DAI GitHub release\npage](//github.com/googleads/googleads-ima-android-dai/releases) and download\nthe basic example. This example is an Android app that you can open in Android\nStudio for testing purposes.\n| **Important:** It's important for your app to send ID3 events using [`VideoStreamPlayerCallback.onUserTextReceived()`](//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](//developers.google.com/ad-manager/dynamic-ad-insertion/sdk/android?service=full#videoplayerappsampleadswrapperjava_2).\n\nTo test with a non-Cloud Video Stitcher VOD stream, set the `CONTENT_TYPE`\nconstant in SampleAdsWrapper.java to either `ContentType.VOD_HLS`, or\n`ContentType.VOD_DASH`, to load the appropriate CMS ID and video ID for the\nstream. Then in SampleVideoPlayer.java, set `currentlyPlayingStreamType` to\neither `CONTENT_TYPE_HLS`, or `CONTENT_TYPE_DASH` ,to properly handle the\nmetadata of your chosen stream type.\n\nIf everything is working properly, clicking the play button on the video player\nbegins the short film \"Tears of Steel\", with a pre-roll ad break.\n\nRequest a VOD stream\n--------------------\n\nTo replace the sample stream with your ad stitched VOD stream, use\n[`sdkFactory.createVideoStitcherVodStreamRequest()`](//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)\nto create an ad session with Google Ad Manager. You can use the Google Ad\nManager UI to [locate the generated DAI sessions](//support.google.com/admanager/answer/7257678) for monitoring and debugging.\n\n`sdkFactory.createVideoStitcherVodStreamRequest()` is available on IMA DAI SDK v3.30.0 or higher.\n\nTo use [`sdkFactory.createVideoStitcherVodStreamRequest()`](//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)\nwith the `vodConfigId` parameter, IMA DAI SDK version 3.33.0 or higher is required.\n\nIn the existing sample, there are conditional statements for requesting a VOD\nstream or a livestream. To make it work with the Google Cloud Video Stitcher\nAPI, you need to add a new path to return a `StreamRequest` created using\n`sdkFactory.createVideoStitcherVodStreamRequest()`.\n\nHere's an example:\n\n**videoplayerapp/SampleAdsWrapper.java** \n\n ...\n private enum ContentType {\n LIVE_HLS,\n LIVE_DASH,\n // Add a VOD HLS Google Cloud type. DASH streams are also supported.\n VOD_HLS_GOOGLE_CLOUD,\n VOD_DASH_GOOGLE_CLOUD,\n VOD_HLS,\n VOD_DASH,\n }\n\n // Set CONTENT_TYPE to the associated enum for the\n // stream type you would like to test.\n private static final ContentType CONTENT_TYPE =\n ContentType.VOD_HLS_GOOGLE_CLOUD;\n ...\n\n @Nullable\n private StreamRequest buildStreamRequest() {\n StreamRequest request;\n switch (CONTENT_TYPE) {\n ...\n case VOD_HLS_GOOGLE_CLOUD:\n case VOD_DASH_GOOGLE_CLOUD:\n // VOD HLS or DASH stream generated by the\n // Google Cloud Video Stitcher API.\n request = sdkFactory.createVideoStitcherVodStreamRequest(\n \"\u003cvar scope=\"NETWORK_CODE\" translate=\"no\"\u003eNETWORK_CODE\u003c/var\u003e\",\n \"\u003cvar scope=\"LOCATION\" translate=\"no\"\u003eLOCATION\u003c/var\u003e\",\n \"\u003cvar scope=\"PROJECT_NUMBER\" translate=\"no\"\u003ePROJECT_NUMBER\u003c/var\u003e\",\n \"\u003cvar scope=\"OAUTH_TOKEN\" translate=\"no\"\u003eOAUTH_TOKEN\u003c/var\u003e\",\n \"\u003cvar scope=\"VOD_CONFIG_ID\" translate=\"no\"\u003eVOD_CONFIG_ID\u003c/var\u003e\"\n );\n if (CONTENT_TYPE == ContentType.VOD_HLS_GOOGLE_CLOUD) {\n request.setFormat(StreamFormat.HLS);\n } else {\n request.setFormat(StreamFormat.DASH);\n }\n return request;\n }\n // Content type not selected.\n return null;\n }\n ...\n\nReload the app to request and play your custom VOD stream.\n\n### (Optional) Add streaming session options\n\nCustomize your stream request by adding session options to override the default\nCloud Video Stitcher API configuration using\n[`StreamRequest.setVideoStitcherSessionOptions()`](//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).\nIf you provide an unrecognized option, the Cloud\nVideo Stitcher API will respond with an HTTP 400 error. Consult the\n[troubleshooting guide](//cloud.google.com/video-stitcher/docs/troubleshooting)\nfor assistance.\n\nFor example, you can override the\n[manifest options](//cloud.google.com/video-stitcher/docs/concepts/manifest-options)\nwith the following code snippet, which requests two stream manifests with\nrenditions ordered from lowest bitrate to highest. \n\n public void requestAndPlayAds() {\n adsLoader.addAdErrorListener(this);\n adsLoader.addAdsLoadedListener(this);\n StreamRequest streamRequest = buildStreamRequest();\n // The following session options are examples. Use session options\n // that are compatible with your video stream.\n Map\\\u003cString, Object\\\u003e sessionOptions = Map.of(\n \"manifestOptions\", Map.of(\n \"bitrateOrder\", \"ascending\"\n )\n );\n /\\* sessionOptions JSON structure.\n \\* {\n \\* \"manifestOptions\": {\n \\* \"bitrateOrder\": \"ascending\"\n \\* }\n \\* };\n \\*/\n streamRequest.setVideoStitcherSessionOptions(sessionOptions);\n adsLoader.requestStream(streamRequest);\n }\n\n\u003cbr /\u003e\n\nClean up\n--------\n\nNow that you have successfully hosted a VOD stream\nusing the Google Cloud Video Stitcher API and requested it using the IMA DAI SDK\nfor Android, it's important to clean up any serving\nresources.\n\nFollow the\n\n[VOD clean up](//cloud.google.com/video-stitcher/docs/how-to/gam/vod/clean-up)\nguide to remove any unneeded resources and assets."]]