0

I have one TableViewController and I play video on that Table by tapping button inside cell to play by AVPlayer by code

@IBAction func btnFullScreen(sender: AnyObject) {    
    let playController = AVPlayerViewController()
    self.presentViewController(playController, animated: true, completion: nil)
    playController.player?.play()
}

So, It will play the video in full screen, and also exist with done button on the top left. I want to get event done button, after click the done button it will get the current time of player to do more something else. but how to get the event click done button?

Leang Socheat
  • 37
  • 2
  • 9

2 Answers2

1

Currently, it is not possible that you can intercept the Done button of AVPlayerController, a Bug Report is submitted for this with radar: 27047358

You can only add an observer for AVPlayerItemDidPlayToEndTimeNotification which will be fired when your item finished playing.

Also according to the documentation:

Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behaviour.

Reference - here

Rajat
  • 10,977
  • 3
  • 38
  • 55
  • thank for answer, Can you update to code for your answer?. because I try to use PlayerItemDIdReachEnd, but there is no option or name on swift 2.2 – Leang Socheat Nov 22 '16 at 09:38
  • Sorry, can you try AVPlayerItemDidPlayToEndTimeNotification – Rajat Nov 22 '16 at 09:40
  • This Option cannot control when the button done was tapped. it just control when the video was play full of video length. :( – Leang Socheat Nov 22 '16 at 09:43
  • thats what i have told you in my answer :) – Rajat Nov 22 '16 at 09:43
  • @AhmadF it was possible by overriding the class methods of AVPlayerController which is not a recommended way and it is mentioned in the docs also. – Rajat Nov 22 '16 at 09:52
  • @Rajat I added an incomplete comment and I delete it, just ignore it! I agree with your answer as you can see there is a vote up for it :) – Ahmad F Nov 22 '16 at 10:15
0

I'm using this trick , it works on my scenario but it may give you a clue :

Step one:

add observers on a view controller you want to present your from there player view controller to get notified when its frame changes:

 self.playerViewController.addObserver(self, forKeyPath: #keyPath(MyViewController.view.frame), options: .new, context: nil)
 NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying),
                                               name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.playerViewController.player!.currentItem)

Step Two:

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if (keyPath ==  #keyPath(MyViewController.view.frame)){

        if playerViewController.player?.rate == 0{
            doneClicked()
        }
    }
}

Step Three:

    func doneClicked(){

    self.playerViewController.player!.pause()
    self.playerViewController.player!.rate =  0.0
    self.playerViewController.player = nil
    self.playerViewController.removeObserver(self, forKeyPath: #keyPath(HomeTabViewController.view.frame))
    NotificationCenter.default.removeObserver(self)
    self.playerViewController.dismiss(animated: false, completion: nil)
}
MohyG
  • 1,335
  • 13
  • 25