在实现画中画(PiP)功能时需要注意的技术点

2024-04-25  本文已影响0人  喔牛慢慢爬
  1. 画中画(PiP)在iPad上支持系统iOS9以上的设备,在iPhone上支持系统iOS14以上的设备;
  2. 画中画(PiP)启动成功的前提是播放的视频必须是正在播放状态,视频暂停、加载时都是无法启动的;
  3. 画中画(PiP)启动时App必须是前台活跃状态,App进入后台PiP很可能启动失败
    以上的第2、3条会报错误:

Error Domain=AVKitErrorDomain Code=-1001 "Failed to start picture in picture." UserInfo={NSLocalizedDescription=Failed to start picture in picture., NSLocalizedFailureReason=The UIScene for the content source has an activation state other than UISceneActivationStateForegroundActive, which is not allowed.}

  1. 画中画(PiP)播放窗口的大小无法改变,窗口大小是根据视频大小形状来显示的;
  2. 画中画(PiP)可以隐藏播放窗口的部分播放按钮

    可以通过pipVC.setValue(1, forKey: "controlsStyle")设置
    0 = automatic: 系统会根据上下文自动选择适当的控制样式;
    1 = minimal: 显示最少的控件,仅包含关闭和恢复全屏按钮;
    2 = default: 使用系统的默认控件显示方式;

  3. 画中画(PiP)可以添加自定义view,但是在iOS16.2以后添加自定义View无法响应点击事件;
    可在在代理回调pictureInPictureControllerWillStartPictureInPicture中进行添加
    func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
         print("PIP:即将开启画中画")
         if let window = UIApplication.shared.windows.first {
             let _customView = UIView()            
             let _button = UIButton(type: .custom)
             _button.setTitle("按钮", for: .normal)
             _button.setTitleColor(.white, for: .normal)
             _button.addTarget(self, action: #selector(test), for: .touchUpInside)
             _customView.addSubview(_button)
             
             _button.snp.makeConstraints { make in
                 make.width.equalTo(80)
                 make.height.equalTo(40)
                 make.center.equalToSuperview()
             }
             window.addSubview(_customView)
             _customView.snp.makeConstraints { (make) -> Void in
                 make.edges.equalToSuperview()
             }
         }
     }
    
  4. 设置进入后台时自动开启画中画(PiP)需要使用canStartPictureInPictureAutomaticallyFromInline属性,但是注意此变量只支持iOS14.2以后的系统,并且视频必须是在播放中的;
  5. 苹果官方要求用户主动开启画中画(PiP),不可以编程方式开始画中画(PiP), App Store 审核团队会拒绝不符合此要求的应用程序;
    • 仅在响应用户交互时开始画中画播放,而绝不会以编程方式开始。 App Store 审核团队会拒绝不符合此要求的应用程序


      1
    • 画中画 (PiP) 是一项用户功能,Apple 希望始终由用户控制。 仅在响应用户的明确请求时调用画中画。 如果应用程序以不在用户直接指导下的方式调用画中画,则应用程序商店审核团队将拒绝它。


      2
上一篇下一篇

猜你喜欢

热点阅读