画中画

本指南适用于希望在现有 IMA 实现中添加画中画支持的 IMA 发布商。

前提条件

为您的应用添加画中画支持

自 SDK 3.1.0 版起,IMA 支持 Apple 为 iPad 提供的画中画模式。 如需向应用添加对画中画功能的支持,您需要调整一些设置并实现一些新的 IMA 类,如下所示。

更新设置以允许后台播放

“画中画”模式要求您允许在应用中播放后台媒体。

  1. 音频、AirPlay 和画中画后台模式设置为开启,如下所示:

  2. 设置 AVAudioSession 属性以支持后台播放,并在 IMASettings 中启用后台播放:

    ...
    – (void)viewDidLoad {
     [super viewDidLoad];
    
     self.playButton.layer.zPosition = MAXFLOAT;
    
     [[AVAudioSession sharedInstance] setActive:YES error:nil];
     [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    
     [self setupAdsLoader];
     [self setUpContentPlayer];
    }
    
    – (void)setupAdsLoader {
     IMASettings *settings = [[IMASettings alloc] init];
     settings.enableBackgroundPlayback = YES;
     self.adsLoader = [[IMAAdsLoader alloc] initWithSettings:settings];
     self.adsLoader.delegate = self;
    }

为画中画创建新的 iOS 和 IMA 对象

为了支持画中画功能,Apple 添加了 AVPictureInPictureControllerAVPictureinPictureControllerDelegate 类。IMA 为它添加了 IMAPictureInPictureProxy。如需在您的项目中纳入这些类,请将以下语句添加到您的代码中:

...
@interface VideoViewController () <AVPictureInPictureControllerDelegate,
                                   IMAAdsLoaderDelegate,
                                   IMAAdsManagerDelegate,
                                   UIAlertViewDelegate>
...
// PiP objects.
@property(nonatomic, strong) IMAPictureInPictureProxy *pictureInPictureProxy;
@property(nonatomic, strong) AVPictureInPictureController *pictureInPictureController;
...
@end

- (void)setUpContentPlayer {
  ...
  self.pictureInPictureProxy =
      [[IMAPictureInPictureProxy alloc] initWithAVPictureInPictureControllerDelegate:self];
  self.pictureInPictureController =
      [[AVPictureInPictureController alloc] initWithPlayerLayer:self.contentPlayerLayer];
  self.pictureInPictureController.delegate = self.pictureInPictureProxy;
}

修改广告请求

还有一个要创建的新对象:IMAAVPlayerVideoDisplay。该数据会传递给 IMAAdsRequest 构造函数,以便 SDK 在画中画模式下播放视频时,允许 SDK 将广告插入画中画窗口:

...
- (void)requestAdsWithTag:(NSString *)adTagUrl {
  [self logMessage:@"Requesting ads"];
  // Create an ad request with our ad tag, display container, and optional user context.
  IMAAdsRequest *request = [[IMAAdsRequest alloc]
           initWithAdTagUrl:adTagUrl
         adDisplayContainer:[self createAdDisplayContainer]
       avPlayerVideoDisplay:[[IMAAVPlayerVideoDisplay alloc] initWithAVPlayer:self.contentPlayer]
      pictureInPictureProxy:self.pictureInPictureProxy
                userContext:nil];
  [self.adsLoader requestAdsWithRequest:request];
}

开始播放广告

IMA SDK 广告无法在画中画模式下启动。因此,您需要确保仅在视频处于标准播放模式时调用 [adsManager start]

...
- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdEvent:(IMAAdEvent *)event {
  [self logMessage:@"AdsManager event (%s).", AdEventNames[event.type]];
  // When the SDK notified you that ads have been loaded, play them.
  switch (event.type) {
    case kIMAAdEvent_LOADED:
      if (![self.pictureInPictureController isPictureInPictureActive]) {
        [adsManager start];
      }
      break;
    ...
    default:
      break;
  }
}

进入画中画模式

如果您使用的是不带 AVPlayerViewControllerAVPlayer,则需要添加自己的“画中画”按钮。我们在高级示例中实现了此方法,如下所示:

- (IBAction)onPipButtonClicked:(id)sender {
  if ([self.pictureInPictureController isPictureInPictureActive]) {
    [self.pictureInPictureController stopPictureInPicture];
  } else {
    [self.pictureInPictureController startPictureInPicture];
  }
}

FAQ

如何在视频处于“画中画”模式时启动广告?
当视频处于“画中画”模式时,无法开始播放广告;广告只能在标准播放模式下开始播放。
我现有的画中画集成需要将 self.pictureInPictureController.delegate 设置为我自己的类。如何在画中画模式下实施 IMA 广告并仍是其受托人?
IMA SDK 还需要接收 AVPictureinPictureControllerDelegate 消息,才能在画中画模式下启用广告播放。因此,我们需要您将 AVPictureinPictureController 的委托设置为 IMAPictureInPicturyProxy 的实例。此代理对象会将所有 AVPictureinPictureControllerDelegate 消息转发到您的应用,但也会将调用转发到 IMA,以启用画中画支持。请注意,您还必须为 AVPlayerLayer 维护一个本地句柄。