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

一、实现

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

1、准备画中画
#import <AVKit/AVKit.h>

@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杀死,画中画也立即消失

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景: 画中画(PictureInPicture)在iOS9就已经推出了,不过之前都只能在iPad使用,iPhon...
    一抹相思泪成雨阅读 3,302评论 6 6
  • 1.开启后台模式 2.导入框架#import 创建AVPictureInPictureController注:...
    超级卡布达阅读 15,268评论 8 25
  • 最近开始研究iOS14画中画功能的实现,最终分别通过使用AVPlayerViewController构建播放器和A...
    落叶兮兮阅读 2,660评论 0 5
  • 画中画本质是添加了一个视频悬浮框,demo地址:点我:demo链接[https://github.com/howh...
    howhyone阅读 4,903评论 2 8
  • 一 简述 一种控制器,用于在浮动的可调整大小的窗口中响应用户启动的画中画视频播放。 注意画中画(PiP)是Appl...
    ForgetSou阅读 11,033评论 1 12