子母畫面

本指南適用於想要在現有 IMA 導入項目中加入子母畫面支援功能的 IMA 發布商。

必要條件

在應用程式中新增子母畫面支援功能

自 SDK 3.1.0 版起,IMA 支援 iPad 適用的 Apple 子母畫面模式。 若要在應用程式中支援子母畫面功能,您需要調整幾項設定並導入幾個新的 IMA 類別,如下所示。

正在更新設定,以便啟用背景播放功能

如要使用子母畫面模式,您必須允許在應用程式中播放背景媒體。

  1. 將「Audio、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 在影片以子母畫面模式播放影片時,在子母畫面視窗中插入廣告:

...
- (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];
  }
}

常見問題

影片以子母畫面模式播放時該如何放送廣告?
影片使用子母畫面模式時無法開始廣告,只能在標準播放模式下開始播放。
我現有的子母畫面整合功能必須將 self.pictureInPictureController.delegate 設為自己的類別。我該如何在子母畫面導入 IMA 廣告,同時保有委任資格?
IMA SDK 也需要接收 AVPictureinPictureControllerDelegate 訊息,才能啟用子母畫面模式的廣告播放。因此,我們需要您將 AVPictureinPictureController 的委派項目設為 IMAPictureInPicturyProxy 的執行個體。此 Proxy 物件會將所有 AVPictureinPictureControllerDelegate 訊息轉送至您的應用程式,但也會將呼叫轉送至 IMA,以啟用子母畫面支援。請注意,您也必須維護 AVPlayerLayer 的本機控制代碼。