2

There is one functionality of Video playing Like linkedIn video playing. i am using AVPlayerViewController for playing video.in that i am facing issue that i am not able to detect Close button event when AVPlayerviewController will close. is there any Observer or method which will call when AVPlayerController will dismiss?

Jeff
  • 373
  • 1
  • 6
  • 16
Nimit
  • 145
  • 1
  • 8
  • have a look here: https://stackoverflow.com/questions/38565412/done-button-click-event-in-avplayerviewcontroller , you can try one of the way mentioned there. – Bhavin Kansagara Aug 09 '18 at 07:33

1 Answers1

4

Swift 3.2

import UIKit
import AVKit
import AVFoundation

class PlayResourcesVC: UIViewController {

    //MARK:- Variable Declarations
    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)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    //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)
    }

}
Kamani Jasmin
  • 691
  • 8
  • 11