[iOS ]iPhone上画中画效果实现(iOS14)

前言

今天更新了iOS14,iOS14可以支持画中画功能,闲着无聊,来写个代码实现。

话不多说,先上Demo PictureInPictureDemo
效果图
IMG_3388.PNG

IMG_3387.PNG
基本使用
1、打开Xcode,开启后台模式
image.png
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(@"关闭画中画且恢复播放界面");
}

你可能感兴趣的:([iOS ]iPhone上画中画效果实现(iOS14))