privatesdkFactoryImaSdkFactory;...privateenumContentType{LIVE_HLS,LIVE_DASH,// Add a Live HLS Google Cloud type.LIVE_HLS_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.LIVE_HLS_GOOGLE_CLOUD;...@NullableprivateStreamRequestbuildStreamRequest(){StreamRequestrequest;switch(CONTENT_TYPE){...caseLIVE_HLS_GOOGLE_CLOUD:// Live HLS stream generated by the Google Cloud Video Stitcher API.request=sdkFactory.createVideoStitcherLiveStreamRequest("NETWORK_CODE","CUSTOM_ASSET_KEY","LIVE_CONFIG_ID","LOCATION","PROJECT_NUMBER","OAUTH_TOKEN");request.setFormat(StreamFormat.HLS);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 会为每个广告插播时间点插入从广告代码检索到的广告。清单中使用广告标记表示广告插播时间点。广告标记由直播编码器插入。
[[["易于理解","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 livestreams 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\nlivestream for an event registered with the [Google Cloud Video Stitcher\nAPI](//cloud.google.com/video-stitcher/docs/how-to/gam/live/overview), and how to\ninsert an ad break during playback.\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 live 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 credentials for service\n accounts](//cloud.google.com/iam/docs/creating-short-lived-service-account-credentials).\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\nLive config ID\n: The live config ID you specified when creating your livestream event:\n \u003cvar scope=\"LIVE_CONFIG_ID\" class=\"edit\" translate=\"no\"\u003eLIVE_CONFIG_ID\u003c/var\u003e\n\nCustom asset key\n: The Ad Manager custom asset key generated during the process of [creating a\n configuration for a livestream event](//cloud.google.com/video-stitcher/docs/how-to/gam/live/create-live-configs)\n with the Video Stitcher API:\n \u003cvar scope=\"CUSTOM_ASSET_KEY\" class=\"edit\" translate=\"no\"\u003eCUSTOM_ASSET_KEY\u003c/var\u003e\n\n\nDownload the basic example\n--------------------------\n\nDownload and run the [IMA Android DAI Basic\nExample](//github.com/googleads/googleads-ima-android-dai/releases). Click\nthe **play** button on the video player to start the short film \"Tears of\nSteel\", which contains ad breaks every 30 seconds.\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\nRequest a livestream\n--------------------\n\nTo replace the sample stream with your livestream, you need to use the\n[`ImaSdkFactory.createVideoStitcherLiveStreamRequest()`](//developers.google.com/ad-manager/dynamic-ad-insertion/sdk/android/api/reference/com/google/ads/interactivemedia/v3/api/ImaSdkFactoryhtml#createVideoStitcherLiveStreamRequest(java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String))\nto create an ad session with Google Ad Manager. You can use the Google Ad\nManager UI to [locate the generated DAI\nsessions](//support.google.com/admanager/answer/7257678) for monitoring and\ndebugging purposes.\n\nIn the existing sample, there are conditional statements for requesting a VOD\nstream or a livestream. To make it work with Google Cloud Video Stitcher API,\nyou need to add a new path to return a `StreamRequest` created using\n[`ImaSdkFactory.createVideoStitcherLiveStreamRequest()`](//developers.google.com/ad-manager/dynamic-ad-insertion/sdk/android/api/reference/com/google/ads/interactivemedia/v3/api/ImaSdkFactoryhtml#createVideoStitcherLiveStreamRequest(java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String)).\n\nHere's an example:\n\n#### videoplayerapp/SampleAdsWrapper.java\n\n private sdkFactory ImaSdkFactory;\n ...\n private enum ContentType {\n LIVE_HLS,\n LIVE_DASH,\n // Add a Live HLS Google Cloud type.\n LIVE_HLS_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.LIVE_HLS_GOOGLE_CLOUD;\n ...\n\n @Nullable\n private StreamRequest buildStreamRequest() {\n StreamRequest request;\n switch (CONTENT_TYPE) {\n ...\n case LIVE_HLS_GOOGLE_CLOUD:\n // Live HLS stream generated by the Google Cloud Video Stitcher API.\n request = sdkFactory.createVideoStitcherLiveStreamRequest(\n \"\u003cvar scope=\"NETWORK_CODE\" translate=\"no\"\u003eNETWORK_CODE\u003c/var\u003e\",\n \"\u003cvar scope=\"CUSTOM_ASSET_KEY\" translate=\"no\"\u003eCUSTOM_ASSET_KEY\u003c/var\u003e\",\n \"\u003cvar scope=\"LIVE_CONFIG_ID\" translate=\"no\"\u003eLIVE_CONFIG_ID\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 );\n request.setFormat(StreamFormat.HLS);\n return request;\n }\n // Content type not selected.\n return null;\n }\n ...\n\nReload the app to request and play your custom livestream.\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\nInsert an ad break\n------------------\n\nThe Google Cloud Video Stitcher API inserts ads retrieved from the ad tag for each\nad break. Ad breaks are denoted in the manifest using ad markers. Ad markers are\ninserted by the live stream encoder.\n\n- If you are using your own live stream, you need to insert the ad marker. For\n more information on the supported HLS and DASH ad markers, see the\n [ad markers documentation](//cloud.google.com/video-stitcher/docs/concepts/ad-markers).\n\n- If you created a live stream using the Google Cloud Livestream API,\n [insert an ad break channel event](//cloud.google.com/livestream/docs/how-to/create-channel-events#create_channel_event).\n\nThe ad is played immediately after the ad break is inserted.\n\nClean up\n--------\n\nNow that you have successfully hosted a live 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[livestream clean up](//cloud.google.com/video-stitcher/docs/how-to/gam/live/clean-up)\nguide to remove any unneeded resources and assets."]]