How do I intercept tapping of the done button in AVPlayerViewController?

前端 未结 10 741
渐次进展
渐次进展 2021-01-03 22:45

I created an AVPlayerViewController and an attached AVPlayer in the viewDidAppear method of a custom UIViewController. Ho

10条回答
  •  清歌不尽
    2021-01-03 23:06

    I solved by keeping a weak reference to the AVPlayerViewController instance, and monitoring with a timer where the reference change to nil.

    private weak var _playerViewController : AVPlayerViewController? // global reference
        ...
        ...
        let playerController = AVPlayerViewController() // local reference
        ...
        self.present(playerController, animated: true) { [weak self] in
            playerController.player?.play()
            self?._playerViewController = playerController
            // schedule a timer to intercept player dismissal
            Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] (timer) in
            if self?.playerViewController == nil {
                // player vc dismissed
                timer.invalidate()
            }
    }
    

提交回复
热议问题