在 iOS 上使用 IMA DAI SDK

播放使用 Google Cloud Video Stitcher API 注册的 VOD 串流

本指南演示了如何使用适用于 iOS 的 IMA DAI SDK 请求和播放 Google Cloud VOD 流式传输会话

本指南对 IMA DAI 入门指南中的基本示例进行了扩展。

如需了解如何与其他平台集成或如何使用 IMA 客户端 SDK,请参阅互动式媒体广告 SDK

设置 Google Cloud 项目

输入以下变量以在 IMA SDK 中使用:

位置
Google Cloud 区域 创建 VOD 配置的位置: LOCATION
项目编号
使用 Video Stitcher API 的 Google Cloud 项目编号: PROJECT_NUMBER
OAuth 令牌

具有视频拼接器用户的服务账号的短期有效 OAuth 令牌 角色:

OAUTH_TOKEN

详细了解如何创建短期有效的 OAuth 令牌。 该 OAuth 令牌可以跨多个请求重复使用,只要它尚未 已过期。

广告资源网代码

用于请求广告的 Ad Manager 广告资源网代码: NETWORK_CODE

VOD 配置 ID

VOD 视频流的 VOD 配置 ID:VOD_CONFIG_ID

如需详细了解如何创建 VOD 配置 ID,请参阅云拼接、创建 VOD 配置指南

用户情境
用于跟踪请求的用户上下文。可以是 nil本指南中默认为 nil

设置基本示例

前往 IMA iOS DAI GitHub 版本页面,下载基本 Objective-C 示例。此示例是一个 iOS Xcode 项目,它依赖于 Cocoapods 来加载 IMA DAI SDK。如果您使用的是 Swift,则 IMA 没有 iOS 示例应用,但您可以在本指南后面的 Swift 代码段部分查看 如何在您自己的应用中实现 IMAVideoStitcherVODStreamRequest

如需准备运行示例,请确保已安装 CocoaPods,然后在终端中打开基本示例的文件夹并运行以下命令:

pod install --repo-update

该命令完成后,您会在项目文件夹中看到一个名为 BasicExample.xcworkspace 的文件。在 Xcode 中打开此文件并运行示例,以确保 确保测试视频和广告按预期播放

请求 VOD 视频流

如需将选段串流替换为广告拼接的 VOD 串流,请使用 IMAVideoStitcherVODStreamRequest 通过 Google Ad Manager 创建广告会话。您可以使用 Google Ads 用于找到生成的 DAI 会话以进行监控和调试的管理器界面。

现有示例中提供了从 Google 的 DAI 服务器请求 VOD 流或直播的示例。为了使其与 Google Cloud Video Stitcher API 搭配使用,您需要将当前的 requestStream 函数替换为使用 IMAVideoStitcherVODStreamRequest 类的函数。

示例如下:

Objective-C

ViewController.m

...

#import <GoogleInteractiveMediaAds/GoogleInteractiveMediaAds.h>
/// The Stream's VOD config ID.
static NSString *const kVODConfigID = @"VOD_CONFIG_ID";
/// The Network Code used by the Video Stitcher API.
static NSString *const kNetworkCode = @"NETWORK_CODE";
/// The Google Cloud project using the Video Stitcher API.
static NSString *const kProjectNumber = @"PROJECT_NUMBER";
/// The Google Cloud region containing your project.
static NSString *const kLocation = @"LOCATION";
/// An OAuth Token created by a Google Cloud account with Video Stitcher API
/// permissions.
static NSString *const kOAuthToken = @"OAUTH_TOKEN";

/// Fallback URL in case something goes wrong in loading the stream. If all goes well, this will not
/// be used.
static NSString *const kBackupStreamURLString =
    @"http://googleimadev-vh.akamaihd.net/i/big_buck_bunny/bbb-,480p,720p,1080p,.mov.csmil/"
    @"master.m3u8";
@interface ViewController () <IMAAdsLoaderDelegate, IMAStreamManagerDelegate>

...

- (void)requestStream {
    // Create an ad display container for ad rendering.
    IMAAdDisplayContainer *adDisplayContainer =
        [[IMAAdDisplayContainer alloc] initWithAdContainer:self.videoView
                                            viewController:self
                                            companionSlots:nil];
    // Create an IMAAVPlayerVideoDisplay to give the SDK access to your video player.
    IMAAVPlayerVideoDisplay *imaVideoDisplay =
        [[IMAAVPlayerVideoDisplay alloc] initWithAVPlayer:self.contentPlayer];
    IMAVideoStitcherVODStreamRequest *streamRequest =
        [[IMAVideoStitcherVODStreamRequest alloc] initWithVODConfigID:kVODConfigID
                                                             region:kLocation
                                                      projectNumber:kProjectNumber
                                                         OAuthToken:kOAuthToken
                                                        networkCode:kNetworkCode
                                                 adDisplayContainer:adDisplayContainer
                                                       videoDisplay:imaVideoDisplay
                                                        userContext:nil
                                        videoStitcherSessionOptions:nil];
    [self.adsLoader requestStreamWithRequest:streamRequest];
}

...

Swift

ViewController.swift

...

class ViewController: UIViewController, IMAAdsLoaderDelegate, IMAStreamManagerDelegate {
  /// The Stream's VOD config ID.
  static let vodConfigID = "VOD_CONFIG_ID"
  /// The Network Code used by the Video Stitcher API.
  static let networkCode = "NETWORK_CODE"
  /// The Google Cloud project using the Video Stitcher API.
  static let projectNumber = "PROJECT_NUMBER"
  /// The Google Cloud region containing your project.
  static let location = "LOCATION"
  /// An OAuth Token created by a Google Cloud account with Video Stitcher API
  /// permissions.
  static let oAuthToken = "OAUTH_TOKEN"

  /// Fallback URL in case something goes wrong in loading the stream. If all goes well, this will
  /// not be used.
  static let backupStreamURLString = """
    http://googleimadev-vh.akamaihd.net/i/big_buck_bunny/\
    bbb-,480p,720p,1080p,.mov.csmil/master.m3u8
    """

  ...

  func requestStream() {
    // Create an ad display container for ad rendering.
    adDisplayContainer = IMAAdDisplayContainer(
      adContainer: videoView,
      viewController: self,
      companionSlots: nil)
    // Create an IMAAVPlayerVideoDisplay to give the SDK access to your video player.
    let imaVideoDisplay = IMAAVPlayerVideoDisplay(avPlayer: contentPlayer!)
    // Create a VOD stream request.
    let streamRequest = IMAVideoStitcherVODStreamRequest(
      VODConfigID: ViewController.vodConfigID,
      region: ViewController.location,
      projectNumber: ViewController.projectNumber,
      oAuthToken: ViewController.oAuthToken,
      networkCode: ViewController.networkCode,
      adDisplayContainer: adDisplayContainer!,
      videoDisplay: imaVideoDisplay,
      userContext: nil,
      videoStitcherSessionOptions: nil)
    adsLoader?.requestStream(with: streamRequest)
  }

  ...

运行项目,然后您就可以请求并播放自定义 VOD 串流。

(可选)添加直播会话选项

通过在 IMAVideoStitcherVODStreamRequest 中填充 videoStitcherSessionOptions 参数,添加会话选项以替换默认的 Cloud Video Stitcher API 配置,从而自定义数据流请求。 如果您提供了无法识别的选项, Video Stitcher API 将返回 HTTP 400 错误作为响应。如需帮助,请参阅问题排查指南

例如,您可以替换 清单选项 该代码段可请求两个流式传输清单, 将视频流从低到高排序

Objective-C

 // Define session options JSON string.
 // The following session options are examples. Use session options
 // that are compatible with your video stream.
 NSString *sessionOptionsStr =
   @"{"
   "  \"manifestOptions\": {"
   "    \"includeRenditions\":["
   "      {\"bitrateBps\": 3000, \"codecs\": \"hvc1.1.4.L126.B0, mp4a.40.2\"},"
   "      {\"bitrateBps\": 2000, \"codecs\": \"avc1.64001f, mp4a.40.2\"},"
   "    ]"
   "  }"
   "}";
 // convert JSON NSString to NSDictionary
 NSData *sessionOptionsData = [sessionOptionsStr dataUsingEncoding:NSUTF8StringEncoding];
 NSError *error = nil;
 NSDictionary *sessionOptions = [NSJSONSerialization
                       JSONObjectWithData:sessionOptionsData
                       options:0
                       error:&error];
 // make stream request
 IMAVideoStitcherVODStreamRequest *streamRequest =
     [[IMAVideoStitcherVODStreamRequest alloc] initWithVODConfigID:kVODConfigID
                                                           region:kLocation
                                                     projectNumber:kProjectNumber
                                                       OAuthToken:kOAuthToken
                                                       networkCode:kNetworkCode
                                               adDisplayContainer:adDisplayContainer
                                                     videoDisplay:imaVideoDisplay
                                                       userContext:nil
                                       videoStitcherSessionOptions:sessionOptions];
 [self.adsLoader requestStreamWithRequest:streamRequest];

Swift

 // Define session options JSON string.
 // The following session options are examples. Use session options
 // that are compatible with your video stream.
 let sessionOptionsStr = """
     {
       "manifestOptions": {
         "includeRenditions": [
           {"bitrateBps": 3000, "codecs": "hvc1.1.4.L126.B0, mp4a.40.2"},
           {"bitrateBps": 2000, "codecs": "avc1.64001f, mp4a.40.2"},
         ],
         "bitrateOrder": "ascending"
       }
     }
     """
 // convert JSON string to dictionary
 guard let sessionOptionsData = sessionOptionsStr.data(using: .utf8, allowLossyConversion: false) else { return nil }
 let sessionOptions = try? JSONSerialization.jsonObject(with: sessionOptionsData, options: .mutableContainers)
 // make stream request
 let streamRequest = IMAVideoStitcherVODStreamRequest(
   vodConfigID:ViewController.vodConfigID
   region:ViewController.location
   projectNumber:ViewController.projectNumber
   OAuthToken:ViewController.oAuthToken
   networkCode:ViewController.networkCode
   adDisplayContainer:adDisplayContainer
   videoDisplay:imaVideoDisplay
   userContext:nil
   videoStitcherSessionOptions:sessionOptions)
 adsLoader?.requestStream(with: streamRequest)

清理

现在,您已成功使用 Google Cloud Video Stitcher API 托管 VOD 流式传输,并使用适用于 iOS 的 IMA DAI SDK 请求了该流式传输,请务必清理所有广告投放资源。

按照 VOD 清理 移除任何不需要的资源和资产的指南。