支援子母畫面
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
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 的本機控制代碼。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eThis guide explains how to add Picture-in-Picture support to your iOS app using the IMA SDK, version 3.1.0 and later.\u003c/p\u003e\n"],["\u003cp\u003eTo enable Picture-in-Picture, you must allow background media playback in your app settings and implement specific IMA and iOS classes like \u003ccode\u003eIMAPictureInPictureProxy\u003c/code\u003e and \u003ccode\u003eAVPictureInPictureController\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe IMA SDK requires the use of \u003ccode\u003eIMAAVPlayerVideoDisplay\u003c/code\u003e for ad insertion within the Picture-in-Picture window and ads can only be started in standard playback mode, not during Picture-in-Picture.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers might need to adjust their ad request and utilize the provided proxy object to manage \u003ccode\u003eAVPictureinPictureControllerDelegate\u003c/code\u003e messages effectively for ad playback and Picture-in-Picture support.\u003c/p\u003e\n"]]],[],null,["# Support Picture in Picture\n\nMedia Ads (IMA) SDK for iOS.\n\n\u003cbr /\u003e\n\nThis guide is intended for IMA publishers who want to add Picture in\nPicture support to their existing IMA implementation.\n\nPrerequisites\n-------------\n\n- Complete the [Get Started\n guide](/interactive-media-ads/docs/sdks/ios/client-side).\n\nAdding picture-in-picture support to your app\n---------------------------------------------\n\nAs of SDK version 3.1.0, IMA supports Apple's Picture in Picture mode for iPad.\nTo add support for Picture in Picture to your app, you need to tweak a few\nsettings and implement a few new IMA classes, as shown below.\n\n### Updating settings to allow background playback\n\nPicture in Picture mode requires that you allow background media playback in\nyour app.\n\n1. Set **Background Modes** to **ON** for **Audio, AirPlay and Picture in\n Picture** as shown below:\n\n2. Set the `AVAudioSession` properties to support background playback, as well\n as enable background playback in `IMASettings`:\n\n ```objective-c\n ...\n – (void)viewDidLoad {\n [super viewDidLoad];\n\n self.playButton.layer.zPosition = MAXFLOAT;\n\n [[AVAudioSession sharedInstance] setActive:YES error:nil];\n [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];\n\n [self setupAdsLoader];\n [self setUpContentPlayer];\n }\n\n – (void)setupAdsLoader {\n IMASettings *settings = [[IMASettings alloc] init];\n settings.enableBackgroundPlayback = YES;\n self.adsLoader = [[IMAAdsLoader alloc] initWithSettings:settings];\n self.adsLoader.delegate = self;\n }\n ```\n\n### Creating new iOS and IMA objects for picture-in-picture\n\nTo support Picture in Picture, Apple added the `AVPictureInPictureController`\nand `AVPictureinPictureControllerDelegate` classes. IMA, for its part, added\n`IMAPictureInPictureProxy`. To incorporate these classes in your project, add\nthe following statements to your code: \n\n```objective-c\n...\n@interface VideoViewController () \u003cAVPictureInPictureControllerDelegate,\n IMAAdsLoaderDelegate,\n IMAAdsManagerDelegate,\n UIAlertViewDelegate\u003e\n...\n// PiP objects.\n@property(nonatomic, strong) IMAPictureInPictureProxy *pictureInPictureProxy;\n@property(nonatomic, strong) AVPictureInPictureController *pictureInPictureController;\n...\n@end\n\n- (void)setUpContentPlayer {\n ...\n self.pictureInPictureProxy =\n [[IMAPictureInPictureProxy alloc] initWithAVPictureInPictureControllerDelegate:self];\n self.pictureInPictureController =\n [[AVPictureInPictureController alloc] initWithPlayerLayer:self.contentPlayerLayer];\n self.pictureInPictureController.delegate = self.pictureInPictureProxy;\n}\n```\n\n### Modifying your ads request\n\nThere's one more new object to create: `IMAAVPlayerVideoDisplay`. This is passed\nto your `IMAAdsRequest` constructor and allows the SDK to insert ads into the PiP\nwindow when your video is playing in Picture in Picture mode: \n\n```objective-c\n...\n- (void)requestAdsWithTag:(NSString *)adTagUrl {\n [self logMessage:@\"Requesting ads\"];\n // Create an ad request with our ad tag, display container, and optional user context.\n IMAAdsRequest *request = [[IMAAdsRequest alloc]\n initWithAdTagUrl:adTagUrl\n adDisplayContainer:[self createAdDisplayContainer]\n avPlayerVideoDisplay:[[IMAAVPlayerVideoDisplay alloc] initWithAVPlayer:self.contentPlayer]\n pictureInPictureProxy:self.pictureInPictureProxy\n userContext:nil];\n [self.adsLoader requestAdsWithRequest:request];\n}\n```\n\n### Starting ads\n\nIMA SDK ads **cannot be started during picture in picture mode** . As a result,\nyou need to ensure that you only call `[adsManager start]` when your video is in\nstandard playback mode: \n\n```objective-c\n...\n- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdEvent:(IMAAdEvent *)event {\n [self logMessage:@\"AdsManager event (%s).\", AdEventNames[event.type]];\n // When the SDK notified you that ads have been loaded, play them.\n switch (event.type) {\n case kIMAAdEvent_LOADED:\n if (![self.pictureInPictureController isPictureInPictureActive]) {\n [adsManager start];\n }\n break;\n ...\n default:\n break;\n }\n}\n```\n\n### Entering picture-in-picture mode\n\nIf you're using an `AVPlayer` without an `AVPlayerViewController`, you need to\nadd your own Picture in Picture button. We've implemented one in our [Advanced\nSample](//github.com/googleads/googleads-ima-ios/tree/master/Objective-C/AdvancedExample)\nlike this: \n\n - (IBAction)onPipButtonClicked:(id)sender {\n if ([self.pictureInPictureController isPictureInPictureActive]) {\n [self.pictureInPictureController stopPictureInPicture];\n } else {\n [self.pictureInPictureController startPictureInPicture];\n }\n }\n\nFAQ\n---\n\nHow do I start ads when the video is in Picture in Picture mode?\n: Ads cannot be started while the video is in Picture in Picture mode; they\n can only be started in standard playback mode.\n\nMy existing Picture in Picture integration needs to set `self.pictureInPictureController.delegate` to my own class. How can I implement IMA ads in Picture in Picture and still be the delegate?\n: The IMA SDK also needs to receive `AVPictureinPictureControllerDelegate`\n messages to enable ad playback in Picture in Picture mode. This is why we ask\n you to set the delegate for `AVPictureinPictureController` to an instance of\n `IMAPictureInPicturyProxy`. This proxy object forwards on all\n `AVPictureinPictureControllerDelegate` messages to your app, but also forwards\n the calls to IMA to enable Picture in Picture support. Note that you must also\n maintain a local handle to your AVPlayerLayer."]]