3

Is there any method to identify when we close the AVPlayerViewController when the video is still playing?

Attached the image as to which 'Close' button I am referring to. enter image description here

1 Answers1

-1

try this :-

    import UIKit
    import AVKit
    import AVFoundation
    
    class VC: UIViewController {
        
        var video_Url:String = String()
        let playerController = AVPlayerViewController()
        
        
        //MARK:- ViewDidload
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let player = AVPlayer(url: URL(string: video_Url)!)
            playerController.player = player
            self.present(playerController, animated: false) {
                player.play()
                self.playerController.addObserver(self, forKeyPath: #keyPath(UIViewController.view.frame), options: [.old, .new], context: nil)
            }
        }
        
        override func viewDidDisappear(_ animated: Bool) {
            NotificationCenter.default.removeObserver(NSNotification.Name.AVPlayerItemDidPlayToEndTime)
        }
        
        //MARK:- All Method
        func playerDidFinishPlaying(note: NSNotification) {
            self.navigationController?.popViewController(animated: false)
        }
        
        override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
            self.playerController.removeObserver(self, forKeyPath: #keyPath(UIViewController.view.frame))
            self.navigationController?.popViewController(animated: false)
        }
        
    }
Shivam Parmar
  • 1,520
  • 11
  • 27