0

我在单元格中有一个视频,如果我使用按钮将其置于 PIP 模式,一切正常,但如果我以编程方式在单元格退出屏幕时不会自动进入 PIP 模式,则可以通过激活它按钮?

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    guard let cell = self.playerCell, self.playerController != nil else {
        return
    }
    
    let rect = self.view.convert(cell.frame, from: scrollView)
    if rect.origin.y < 0, self.pipController == nil {
        self.pipController = self.startPictureInPicture()
    }
}

func startPictureInPicture() -> AVPictureInPictureController? {
    guard AVPictureInPictureController.isPictureInPictureSupported() else {
        return nil
    }
    
    let layer = AVPlayerLayer(player: self.playerController.player)
    try? AVAudioSession.sharedInstance().setActive(true)
    if let pipController = AVPictureInPictureController(playerLayer: layer) {
        if pipController.isPictureInPicturePossible {
            pipController.startPictureInPicture()
        } else {
            pipController.addObserver(self, forKeyPath: "isPictureInPicturePossible", options: [.new, .initial], context: nil)
        }
    }
    
    return nil
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "isPictureInPicturePossible", let pipController = object as? AVPictureInPictureController, pipController.isPictureInPicturePossible {
        pipController.startPictureInPicture()
    }
}

更新:调试控制台总是显示警告Unbalanced calls to begin/end appearance transitions for <UIViewController>。但这我已经解决了:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    pipController.startPictureInPicture()
}
4

1 回答 1

1

根据 Apple 的指导方针,我们不应该在没有用户操作的情况下启动 PIP 模式。

仅响应用户交互开始画中画播放,从不以编程方式播放。App Store 审核团队拒绝不符合此要求的应用程序。

来源:https ://developer.apple.com/documentation/avkit/adopting_picture_in_picture_in_a_custom_player

我建议您在应用程序中创建自己的自定义 PIP 视图以避免被拒绝。

于 2021-04-26T20:33:16.400 回答