支持画中画
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
iOS 版媒体广告 (IMA) SDK。
本指南面向希望在其现有 IMA 实现中添加画中画支持的 IMA 发布商。
前提条件
为应用添加画中画支持
自 SDK 版本 3.1.0 起,IMA 支持 iPad 的画中画模式。如需为应用添加画中画支持,您需要调整一些设置并实现一些新的 IMA 类,如下所示。
更新设置以允许后台播放
画中画模式要求您允许应用在后台播放媒体。
将后台模式设置为开启,以启用音频、AirPlay 和画中画,如下所示:

设置 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
的实例。此代理对象会将所有 AVPictureinPictureControllerDelegate
消息转发到您的应用,还会将调用转发到 IMA 以启用画中画支持。请注意,您还必须维护 AVPlayerLayer 的本地句柄。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):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."]]