今天爱分享给大家带来[iOS14]iPhone上画中画效果实现[详细代码],希望能够帮助到各位。
Demo PictureInPictureDemo
效果图
基本使用
1、打开Xcode,开启后台模式
2、打开画中画权限
-(void)openAccess{ @try { NSError *error = nil; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error]; [[AVAudioSession sharedInstance] setActive:YES error:&error]; } @catch (NSException *exception) { NSLog(@"AVAudioSession发生错误"); } }
3、初始化画中画控制器
-(void)setupPictureInPicture{ NSURL *urlVideo = [[NSBundle mainBundle]URLForResource:@"v1" withExtension:@"MP4"]; AVAsset *asset = [AVAsset assetWithURL:urlVideo]; AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset]; self.player = [AVPlayer playerWithPlayerItem:playerItem]; self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; self.playerLayer.frame = self.view.frame; self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspect; [self.view.layer addSublayer:self.playerLayer]; //1.判断是否支持画中画功能 if ([AVPictureInPictureController isPictureInPictureSupported]) { self.pipVC = [[AVPictureInPictureController alloc] initWithPlayerLayer:self.playerLayer]; self.pipVC.delegate = self; } [self.player play]; }
4、创建按钮
-(void)creatBtn{ UIButton * switchBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [switchBtn setFrame:CGRectMake(20, 20, 50, 40)]; [switchBtn setBackgroundColor:[UIColor blackColor]]; [switchBtn setImage:[UIImage imageNamed:@"Classcenter_draw"] forState:UIControlStateNormal]; [switchBtn addTarget:self action:@selector(switchBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview: switchBtn]; }
5、按钮点击方法实现
-(void)switchBtnClick:(UIButton*)sender{ //判断当前是否为画中画 if (self.pipVC.isPictureInPictureActive) { //关闭画中画 [self.pipVC stopPictureInPicture]; } else { //开始画中画 [self.pipVC startPictureInPicture]; } }
6、AVPictureInPictureControllerDelegate代理实现
// 即将开启画中画 - (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController{ NSLog(@"即将开启画中画"); } // 已经开启画中画 - (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController{ NSLog(@"已经开启画中画"); } // 开启画中画失败 - (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error{ NSLog(@"开启画中画失败"); } // 即将关闭画中画 - (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController{ NSLog(@"即将关闭画中画"); } // 已经关闭画中画 - (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController{ NSLog(@"已经关闭画中画"); } // 关闭画中画且恢复播放界面 - (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler{ NSLog(@"关闭画中画且恢复播放界面"); }