在OS13一下的iPad中的启用画中画必须在视频未加载成功之前设置,不然画中画会启动失败。但是在OS13以后的版本是不需要考虑这个问题的。
设置画中画代码
- (void)setupPictureInPicturePlayback
{
// 判断playerLayer是否为空
if (self.currentPlayerLayer == nil) {
LDPlayerLog(@"self.currentPlayerManager.currentPlayerLayer为空");
return;
}
///检查当前应用支持画中画模式
if (AVPictureInPictureController.isPictureInPictureSupported) {
self.pictureInPictureController = [[AVPictureInPictureController alloc] initWithPlayerLayer:self.currentPlayerManager.currentPlayerLayer];
self.pictureInPictureController.delegate = self;
} else {
LDPlayerLog(@"当前配置有问题,不支持开启画中画");
}
}
启用画中画的要求就是必须使用系统的的AVPlayerLayer,例如我们使用的CC视频的SDK,内部封装的的就是苹果自己的播放器。在设置播放数据时SDK内部会创建AVPlayerLayer,所以在设置数据的时候初始化画中画。
//播放在线资源
-(void)setVodVideo:(DWVodVideoModel *)videoModel
{
// 设置播放器代理
self.player.delegate = self;
[self.player playVodViedo:videoModel withCustomId:nil];
// 因为CC没有给出指定清晰度的方法,只能在设置完数据后切换清晰度
[self.player switchQuality:self.videoInfoModel.videoQualities[self.clarityIndex] withCustomId:nil];
// 为了设置画中画,因为在iOS13版本以下,必须在视频未加载时设置画中画,但是playerLayer必须初始化完成
if (self.playerSetResourcesToPlay) {
self.playerSetResourcesToPlay(self, self.playerModel);
}
}
/// 播放本地视频
-(void)playLocalVideo:(DWDownloadModel *)downloadModel
{
// 设置播放器代理
self.player.delegate = self;
// 播放本地播放
[self.player playLocalVideo:downloadModel];
// 为了设置画中画,因为在iOS13版本以下,必须在视频未加载时设置画中画,但是playerLayer必须初始化完成
if (self.playerSetResourcesToPlay) {
self.playerSetResourcesToPlay(self, self.playerModel);
}
}
我发现次这种开启画中画的方式还是有几率设置失败,在查询这个问题的时候我也去看了腾讯视频开启画中画的做法,腾讯视频是在开启画中画时,重新加载视频的资源,之后开启画中画,以此来保证画中画启动成功。