在AVPlayerViewControllerDelegate中添加方法,当进入画中画模式或从画中画模式退出时,都需要设置AVPlayerViewController的contentOverlayView为nil,并在从画中画模式退出时调用AVPlayerViewController的方法viewWillDisappear()和viewDidDisappear()。
示例代码如下:
extension ViewController: AVPlayerViewControllerDelegate {
func playerViewController(_ playerViewController: AVPlayerViewController, willBeginPictureInPictureWithAnimationCoordinator animationCoordinator: AVPictureInPictureControllerAnimationCoordinator) {
playerViewController.contentOverlayView?.removeFromSuperview()
playerViewController.contentOverlayView = nil
}
func playerViewController(_ playerViewController: AVPlayerViewController, willEndPictureInPictureWithAnimationCoordinator animationCoordinator: AVPictureInPictureControllerAnimationCoordinator) {
playerViewController.viewWillDisappear(false)
playerViewController.viewDidDisappear(false)
playerViewController.contentOverlayView?.removeFromSuperview()
playerViewController.contentOverlayView = nil
}
}