Done button click event in AVPlayerViewController

Yogendra Patel picture Yogendra Patel · Jul 25, 2016 · Viewed 11.5k times · Source

I want to play local video in AVPlayerViewController but did not find click event of Done button.

My video is able to play in AVPlayerViewController but I did not find next button , previous button and done button click event.

Answer

Vatsal Shukla picture Vatsal Shukla · Sep 27, 2017


I've done this to get Done button click event from AVPlayerViewController.

First of all, Create an extension of Notification.Name like bellow

extension Notification.Name {
static let kAVPlayerViewControllerDismissingNotification = Notification.Name.init("dismissing")
}

Now, Create an extension of AVPlayerViewController and override viewWillDisappear like bellow

// create an extension of AVPlayerViewController
extension AVPlayerViewController {
    // override 'viewWillDisappear'
    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // now, check that this ViewController is dismissing
        if self.isBeingDismissed == false {
            return
        }

        // and then , post a simple notification and observe & handle it, where & when you need to.....
        NotificationCenter.default.post(name: .kAVPlayerViewControllerDismissingNotification, object: nil)
    }
}

THIS IS FOR ONLY BASIC FUNCTIONALITY THAT HANDLES DONE BUTTON'S EVENT.

happy coding...