如何重写代码以检测何时在 WKWebKit 中关闭 AVPlayer

How to rewrite code for detect when AVPlayer is closed inside WKWebKit

我需要检测 AVPlayer 何时关闭。 当我在 webkit 中打开带有视频内容的网站并按下播放器时,AVPlayer 将打开,我可以使用以下代码检测到它:

override func viewDidLoad() {
        super.viewDidLoad()
        webkit.load(request)

 NotificationCenter.default.addObserver(self, selector: #selector(windowDidBecomeVisibleNotification(notif:)), name: NSNotification.Name("UIWindowDidBecomeVisibleNotification"), object: nil)

}

@objc func windowDidBecomeVisibleNotification(notif: Notification) {
        
        
            if let isWindow = notif.object as? UIWindow {
                if (isWindow !== self.view.window) {
                    print("New window did open, check what is the currect URL")
                }
            }
         }

但我无法理解如果我不想在用户关闭此播放器时执行相反的操作,如何重写此代码。

我正在尝试重写这段代码,但我的尝试失败了。

需要创建两个以通知为中心的观察器来检测最大化和最小化 avplayer

 override func viewDidLoad() {
        super.viewDidLoad()

// listen for videos playing in fullscreen
        NotificationCenter.default.addObserver(self, selector: #selector(onDidEnterFullscreen(_:)), name: UIWindow.didBecomeVisibleNotification, object: view.window)

        // listen for videos stopping to play in fullscreen
    NotificationCenter.default.addObserver(self, selector: #selector(onDidLeaveFullscreen(_:)), name: UIWindow.didBecomeHiddenNotification, object: view.window)
}

@objc func onDidEnterFullscreen(_ notification: Notification) {
    print("Enter Fullscreen")
}

@objc func onDidLeaveFullscreen(_ notification: Notification) {
    print("Leave Fullscreen")
}