How can I detect when someone cancels AVPlayer?
Here’s my code to show the video. How can I detect when someone exits the video screen?
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
NotificationCenter.default.addObserver(self, selector: #selector(videoDidEnded), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
NotificationCenter.default.addObserver(self, selector: #selector(videoDidCancel), name: NSNotification.Name.kAVPlayerViewControllerDismissingNotification, object: player.currentItem)
// Modally present the player and call the player's play() method when complete.
present(controller, animated: true) {
Amplitude.instance().logEvent("ac_content_video_start", withEventProperties: [
"Name": self.book.title,
"Length": 10,
"No_upvotes": self.book.starCount,
"Category": self.book.categories as Any
])
player.play()
}
2
Answers
Unfortunately that is not possible and there is no such event.
What you could do is to associate a
UITapGestureRecognizer
with the view of theAVPlayerViewController
where you can capture the stop/pause/cancel events of yourAVPlayerViewController
. Then in your selector you can handle the pause/stop/cancel.There is no specific
delegate
ornotification
that I know of, however I could offer this workaround.1. Conformance to the UIViewControllerTransitioningDelegate
Make the
UIViewController
that presents the AVPlayerViewController conform to the transitioning delegate2. Add a var to keep track of video status
3. AVPlayerController SetUp
Make the view controller presenting the
AVPlayerController
thetransitioningDelegate
of theAVPlayerController
4. Add this to your movie finish notification handler
This way you know, the movie finished
5. Finally, implement the transition delegate function