一 简述
一种控制器,用于在浮动的可调整大小的窗口中响应用户启动的画中画视频播放。
API_AVAILABLE(ios(9.0), macos(10.15), tvos(14.0)) API_UNAVAILABLE(watchos) @interface AVPictureInPictureController : NSObject
|
注意
画中画(PiP)是Apple希望始终在用户控制下的一项用户功能。仅在响应用户的明确请求时才调用PiP。如果某个应用以非用户直接指导的方式调用PiP,则App Store审核小组将拒绝它。
要在iOS中使用画中画,要在Xcode中执行以下步骤:
- 打开后台模式开启 Audio, AirPlay, and Picture in Picture
- 为音频会话配置合适的类别,如
AVAudioSessionCategoryPlayback
、AVAudioSessionCategoryPlayAndRecord
等
重要
不支持子类化和重写其方法,这会导致未定义的行为。
二 官方属性方法
+ (BOOL)isPictureInPictureSupported;
- (nullable instancetype)initWithPlayerLayer:(AVPlayerLayer *)playerLayer NS_DESIGNATED_INITIALIZER;
@property (nonatomic, readonly) AVPlayerLayer *playerLayer;
@property (nonatomic) BOOL requiresLinearPlayback API_AVAILABLE(ios(14.0), macos(11.0), tvos(14.0)) API_UNAVAILABLE(watchos);
@property (nonatomic, weak, nullable) id <AVPictureInPictureControllerDelegate> delegate;
@property (nonatomic, readonly, getter = isPictureInPicturePossible) BOOL pictureInPicturePossible;
@property (nonatomic, readonly, getter = isPictureInPictureActive) BOOL pictureInPictureActive;
@property (nonatomic, readonly, getter = isPictureInPictureSuspended) BOOL pictureInPictureSuspended;
- (void)startPictureInPicture;
- (void)stopPictureInPicture;
@property (nonatomic, readonly) BOOL canStopPictureInPicture API_AVAILABLE(tvos(14.0)) API_UNAVAILABLE(ios, macos, watchos);
@property (class, nonatomic, readonly) UIImage *pictureInPictureButtonStartImage API_AVAILABLE(ios(13.0), tvos(14.0));
@property (class, nonatomic, readonly) UIImage *pictureInPictureButtonStopImage API_AVAILABLE(ios(13.0), tvos(14.0));
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler;
- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error;
- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
|
三 简单示例
1. 导入框架`#import <AVKit/AVKit.h>`
2. 创建画中画对象
if ([AVPictureInPictureController isPictureInPictureSupported]) { @try { NSError *error = nil; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error]; [[AVAudioSession sharedInstance] setActive:YES error:&error]; } @catch (NSException *exception) { NSLog(@"AVAudioSession错误"); } self.pip = [[AVPictureInPictureController alloc] initWithPlayerLayer:self.player]; self.pip.delegate = self; }
|
注意:上述代码里的self.player
必须是AVPlayerLayer
类型。
3. 开启/关闭
if (self.pip.isPictureInPictureActive) { [self.pip stopPictureInPicture]; } else { [self.pip startPictureInPicture]; }
|
4. 实现AVPictureInPictureControllerDelegate
代理
- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
}
- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
}
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error {
}
- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
}
- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
}
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler {
}
|