根据文档,当 AVPictureInPictureController 关闭时,无论如何关闭,都会调用 willStop 和 didStop 委托方法以及restoreUserInterfaceForPictureInPictureStopWithCompletionHandler。如何判断控制器是通过“X”按钮还是其他按钮关闭以返回常规播放?
X 按钮和恢复按钮之间的区别是:
点击恢复画中画按钮将触发
"pictureInPictureController(_:restoreUserInterfaceForPictureInPictureStopWithCompl etionHandler:)"
"pictureInPictureControllerWillStopPictureInPicture"
"pictureInPictureControllerDidStopPictureInPicture"
点击关闭按钮时将跳过restoreUserInterface回调并直接进入
"pictureInPictureControllerWillStopPictureInPicture"
"pictureInPictureControllerDidStopPictureInPicture"
因此你可以使用 Bool 标志来检查 willStop/DidStop 是否调用了restoreUserInterface。
AVPictureInPictureViewController 中还有一个
pictureInPictureSuspended
属性,但我尝试检查它的值,发现在这两种情况下它总是返回 false,所以我必须使用上面的技巧来检查用户是否点击了恢复或关闭按钮。
如何检测和区分 PIP 关闭和最大化按钮? 使用这个方法:
pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void)
当用户去到不同的地方时,这可以用来手动将播放器返回到原始位置。在这种情况下,如果用户点击“关闭”按钮,它就会关闭,当他们点击“最大化”时,我们就可以随心所欲地控制了。
我的代码示例:
public func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
if let caller = delegate?.getPresentedViewController() {
var presentingVC = caller.presentingViewController
if presentingVC != nil {
return
}
// Maximized Button Clicked
delegate?.PIPdidMaximized()
} else{
completionHandler(true)
} }