iOS 專用互動式媒體廣告 (IMA) SDK。
本指南適用於想在現有 IMA 導入項目中新增子母畫面支援功能的 IMA 發布商。
必要條件
- 完成入門指南的步驟。
在應用程式中新增子母畫面支援
從 SDK 3.1.0 版起,IMA 支援 iPad 的 Apple 子母畫面模式。如要為應用程式新增子母畫面支援,您需要調整一些設定,並實作一些新的 IMA 類別,如下所示。
更新設定,允許背景播放
如要使用子母畫面模式,必須允許應用程式在背景播放媒體。
將「Background Modes」設為「ON」,然後選取「Audio」、「AirPlay」和「Picture in Picture」,如下所示:
設定
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 新增了 AVPictureInPictureController
和 AVPictureinPictureControllerDelegate
類別。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; } }
進入子母畫面模式
如果使用沒有 AVPlayerViewController
的 AVPlayer
,請自行新增子母畫面按鈕。我們已在進階範例中實作一個,如下所示:
- (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 的本機控制代碼。