iOS14画中画实现全流程加各种坑

一、实现

实现画中画很简单,步骤一二三就行。

1、准备画中画
#import 

@property (nonatomic, strong) AVPictureInPictureController *pipVC;
@property (nonatomic, strong) AVPlayerLayer *playerLayer;
@property (nonatomic, strong) AVPlayer *player;
2、配置
- (void)startPip {
    self.player = [AVPlayer playerWithURL:[NSURL URLWithString:self.liveVideo.hdlurl]];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.playerLayer.frame = CGRectMake(100, 80, 100, 200);
    [self.player play];
    [self.view.layer addSublayer:self.playerLayer];
    //1.判断是否支持画中画功能
    if ([AVPictureInPictureController isPictureInPictureSupported]) {
        //2.开启权限
        @try {
            NSError *error = nil;
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback mode:AVAudioSessionModeMoviePlayback options:AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers error:&error];
            // 为什么注释掉这里?你会发现有时 AVAudioSession 会有开启失败的情况。故用上面的方法
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionOrientationBack error:&error];
            [[AVAudioSession sharedInstance] setActive:YES error:&error];
        } @catch (NSException *exception) {
            NSLog(@"AVAudioSession发生错误");
        }
        self.pipVC = [[AVPictureInPictureController alloc] initWithPlayerLayer:self.playerLayer];
        self.pipVC.delegate = self;
    }
}
3、开始/关闭
- (void)openOrClose {
    if (self.pipVC.isPictureInPictureActive) {
        [self.pipVC stopPictureInPicture];
    } else {
        [self.pipVC startPictureInPicture];
    }
}

//各种代理
// 即将开启画中画
- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"");
}
// 已经开启画中画
- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"");
}
// 开启画中画失败
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error {
    NSLog(@"%@", error);
}
// 即将关闭画中画
- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"");
}
// 已经关闭画中画
- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"");
}
// 关闭画中画且恢复播放界面
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler {
    NSLog(@"");
}

二、注意点和坑

  • AVPlayerLayer frame设置后,可调整画中画的大小

  • 画中画底层为AVPlayer,有人说只能播放视频,其实是错的,可以播放.m3u8的直播(目前某音已有该逻辑)

    • 所以存在问题,如果APP中播放器使用其它协议的URL,那么如何切换?一般APP中为SDK提供的播放器。
    • 直播中,若APP中和画中画使用不用协议的URL,其实影响不大,因为画面总是实时的,不存在同步问题,只有UI过度上需要处理。
  • 画中画为AVPictureInPictureController,若要APP内全局显示,需让单例持有

  • 真机上调试!模拟器可玩不转

  • app杀死,画中画也立即消失

(非完善,还待继续学习补充)

你可能感兴趣的:(iOS14画中画实现全流程加各种坑)