iOS目前支持画中画模式,但是官方只支持视频画中画,公司打算做一个提词器的悬浮
1.实现画中画
-(void)play:(NSString*)URLString {
self.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:URLString]];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
[self.playerLayer setVideoGravity:AVLayerVideoGravityResize];
self.playerLayer.hidden = YES;
self.playerLayer.frame = CGRectMake(0, 0, self.playView.frame.size.width, self.playView.frame.size.height);
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[self.playView.layer insertSublayer:self.playerLayer atIndex:0];
//添加画中画相关代码
[self setupSuport];
}
//添加画中画相关代码
-(void)setupSuport {
if([AVPictureInPictureController isPictureInPictureSupported]) {
_AVPictureInPictureController= [[AVPictureInPictureController alloc] initWithPlayerLayer:self.playerLayer]; _AVPictureInPictureController.requiresLinearPlayback = YES; _AVPictureInPictureController.delegate = self;
}else{
}
}
//开启画中画
if([AVPictureInPictureController isPictureInPictureSupported]) {
[selfstopPlay];
if (_AVPictureInPictureController.pictureInPictureActive) {
[_AVPictureInPictureController stopPictureInPicture];
}else{
[_AVPictureInPictureController startPictureInPicture];
}
}else{
[HQToolsshowMessage:NSLocalizedString(@"升级系统版本即可体验悬浮模式!",nil)];
[self dismissViewControllerAnimated:NO completion:^{
if(weakSelf.smaleMytaiBenBlock) {
weakSelf.smaleMytaiBenBlock(nil);
}
}];
}
2.画中画页面加载自定义View
#pragma markpicInpicDelaget
//画中画将要出现
- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
UIWindow *firstWindow = [UIApplication sharedApplication].windows.firstObject;
DLog(@"++++%@",firstWindow);
[firstWindow addSubview:self.playContView];
[self.playContView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(firstWindow);
}];
}
//画中画已经出现
- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
}
//画中画将要消失
- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
}
//画中画已经消息,恢复页面上的显示
- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
[self.playView addSubview:self.playContView];
[self.playContView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(36*LYYratio);
make.left.mas_equalTo(0*LYXratio);
make.right.mas_equalTo(-0*LYXratio);
make.bottom.mas_equalTo(-58*LYXratio);
}];
}
3 隐藏系统的快进和快退按钮
_AVPictureInPictureController.requiresLinearPlayback = YES;
4 接下来的就是不会的了(感谢大家的帮忙了)
a 隐藏系统的播放和暂停和视频进度
b 自定义画中画的大小,目前大小是根据播放的视频的尺寸控制的,不晓得怎么修改
c 在相机进行拍摄的情况下,画中画会黑屏