Home > Software engineering >  How to detect when someone cancels AVPLayer?
How to detect when someone cancels AVPLayer?

Time:02-18

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

CodePudding user response:

Unfortunately that is not possible and there is no such event.

What you could do is to associate a UITapGestureRecognizer with the view of the AVPlayerViewController where you can capture the stop/pause/cancel events of your AVPlayerViewController. Then in your selector you can handle the pause/stop/cancel.

CodePudding user response:

There is no specific delegate or notification that I know of, however I could offer this workaround.

1. Conformance to the UIViewControllerTransitioningDelegate

Make the `UIViewController** than presents the AVPlayerViewController conform to the transitioning delegate

class YourVC: UIViewController, UIViewControllerTransitioningDelegate

2. Add a var to keep track of video status

// Again, in the VC that presents the AVPlayerViewController
var hasMovieFinished = false

3. AVPlayerController SetUp

Make the view controller presenting the AVPlayerController the transitioningDelegate of the AVPlayerController

let controller = AVPlayerViewController()

// Add this
controller.transitioningDelegate = self

controller.player = player

4. Add this to your movie finish notification handler

This way you know, the movie finished

@objc
func videoDidEnded() {
    print("Video finished")
    hasMovieFinished = true
    
    // up to you if you dismiss the controller or not
    dismiss(animated: true) {
        // do what you want
    }
}

5. Finally, implement the transition delegate function

// MARK: UIViewControllerTransitioningDelegate
// Gets called when a modal was dismissed
func animationController(forDismissed dismissed: UIViewController)
-> UIViewControllerAnimatedTransitioning?
{
    // The dismissal was before the movie ended
    if !hasMovieFinished
    {
        print("Movie was cancelled")
        // DO WHAT YOU WANT, MOVIE WAS CANCELLED
    }
    
    return nil
}
  • Related