How to detect when AVPlayer video ends playing?

avplayer did finish playing objective c
avplayer start playing notification swift
avplayer start playing notification objective-c
avplayer is playing
avplayer not playing video
swift video player
avplayer streaming video example
avplayer change video quality

I'am using AVPlayer for playing local video file (mp4) in Swift. Does anyone know how to detect when video finish with playing? Thanks

To get the AVPlayerItemDidPlayToEndTimeNotification your object needs to be an AVPlayerItem.

To do so, just use the .currentItem property on your AVPlayer

Now you will get a notification once the video ends!

See my example:

let videoPlayer = AVPlayer(URL: url)       

NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:",
        name: AVPlayerItemDidPlayToEndTimeNotification, object: videoPlayer.currentItem)

func playerDidFinishPlaying(note: NSNotification) {
    print("Video Finished")
}

Swift 3

let videoPlayer = AVPlayer(URL: url)       

NotificationCenter.default.addObserver(self, selector: Selector(("playerDidFinishPlaying:")), 
       name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: videoPlayer.currentItem)

func playerDidFinishPlaying(note: NSNotification) {
    print("Video Finished")
}

Don't forget to remove the Observer in your deinit

Swift 4, 5

NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: .AVPlayerItemDidPlayToEndTime, object: nil)

How to use Swift to detect when AVPlayer video ends playing?, To detect the end of a video in swift we'll need to create a video player, then use notifications to detect when the video stops playing. We'll do  To detect the end of a video in swift we’ll need to create a video player, then use notifications to detect when the video stops playing. We’ll do this with help of an example in swift. Let’s create a project and drag and drop any video with extension “mp4”, select copy resource if required and add to the target of our project.

Swift 3.0

let videoPlayer = AVPlayer(URL: url)

NotificationCenter.default.addObserver(self, selector:#selector(self.playerDidFinishPlaying(note:)),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)

@objc func playerDidFinishPlaying(note: NSNotification){
        print("Video Finished")
    }

ios How to detect when AVPlayer video ends playing?, ios How to detect when AVPlayer video ends playing? AVPlayerItem AVPlayerItemDidPlayToEndTimeNotification let videoPlayer = AVPlayer(URL: url​)  Here 'fullUrl' is the URL of the video and make sure that there would be no space in the URL, You should replace 'Space' with '%20' so that URL will work file. let videoURL = NSURL(string: fullUrl) let player = AVPlayer(url: videoURL! as URL) playerViewController.delegate = self playerViewController.player = player self.present(playerViewController, animated: false) { self.playerViewController.player!.play() NotificationCenter.default.addObserver(self, selector: #selector(yourViewControllerName.

Swift 4.2 Version:

var player: AVPlayer!
  //
  //
// Configure Player
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let filepath: String? = Bundle.main.path(forResource: "selectedFileName", ofType: "mp4")
    if let filepath = filepath {
        let fileURL = URL.init(fileURLWithPath: filepath)
        player = AVPlayer(url: fileURL)
        let playerLayer = AVPlayerLayer(player: player)
        // Register for notification
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(playerItemDidReachEnd),
                                                         name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                                                         object: nil) // Add observer

        playerLayer.frame = self.view.bounds
        self.view.layer.addSublayer(playerLayer)
        player.play()
    }
}
// Notification Handling
@objc func playerItemDidReachEnd(notification: NSNotification) {
    player.seek(to: CMTime.zero)
    player.play()
}
// Remove Observer
deinit {
    NotificationCenter.default.removeObserver(self)
}

How to detect when AVPlayer video ends playing?, I'am using AVPlayer for playing local video file (mp4) in Swift. Does anyone know how to detect when video finish with playing? Thanks. Playing Streaming Video With AVPlayer. Ask Question Asked 2 years, 5 months ago. Active 8 months ago. How to detect when AVPlayer video ends playing? 8.

For SWIFT 3.0 This is working fine

class PlayVideoViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(PlayVideoViewController.finishVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTimeNotification, object: nil)
    }

    func finishVideo()
    {
        print("Video Finished")
    }
}

How to detect when AVPlayer video ends playing?, I'am using AVPlayer for playing local video file (mp4) in Swift. Does anyone know how to detect when video finish with playing? Thanks. The solution since iOS 10.0 is to use AVPlayerTimeControlStatus which can be observed on AVPlayer timeControlStatus property. The solution prior to iOS 10.0 (9.0, 8.0 etc.) is to roll your own solution. A rate of 0.0 means that the video is paused. When rate != 0.0 it means that the video is either playing or is stalled.

Swift 4.0

This one works for me. Thanks to @Channel

    private func playVideo(fileURL: String) {

            // Create RUL object
            let url = URL(string: fileURL)

            // Create Player Item object
            let playerItem: AVPlayerItem = AVPlayerItem(url: url!)
            // Assign Item to Player
            let player = AVPlayer(playerItem: playerItem)

            // Prepare AVPlayerViewController
            let videoPlayer = AVPlayerViewController()
            // Assign Video to AVPlayerViewController
            videoPlayer.player = player

            NotificationCenter.default.addObserver(self, selector: #selector(myViewController.finishVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)

            // Present the AVPlayerViewController
            present(videoPlayer, animated: true, completion: {
                    // Play the Video
                    player.play()
            })

    }

    @objc func finishVideo()
    {
            print("Video Finished")
    }

Responding to Playback State Changes, I'am using AVPlayer for playing local video file (mp4) in Swift. Does anyone know how to detect when video finish with playing? Thanks. I am working on a site that has a ton of embedded youtube videos, the client wants to show a popup whenever a video stops splaying. I looked at the youtube api and there seems to be a way to detec

How to resume AVPlayer video playback when app becomes active , Observe the Player's State. One of the most important AVPlayerItem properties to observe is status . This property indicates whether the player item  I've written a Xamarin.Forms iOS app Page that users an AVPlayer to play a video via a custom page renderer.. When the video finishes, or when the user scrubs to the end of the video, they should be sent to the next ContentPage in the app.

I'am using AVPlayer for playing local video file (mp4) in Swift. Does anyone know how to detect when video finish with playing? Thanks  An AVPlayer is a controller object used to manage the playback and timing of a media asset. You can use an AVPlayer to play local and remote file-based media, such as QuickTime movies and MP3 audio files, as well as audiovisual media served using HTTP Live Streaming.

I'am using AVPlayer for playing local video file (mp4) in Swift. Does anyone know how to detect when video finish with playing? Thanks. To play a media file, we need to create an AVPlayer instance pointing at the video URL. Then to play/pause, we use these obvious commands: func initializePlayer(videoUrl: URL) { let videoAsset = AVURLAsset(url: videoUrl) let playerItem = AVPlayerItem(asset: videoAsset) player = AVPlayer(playerItem: playerItem) playerLayer = AVPlayerLayer(player

Comments
  • ObjC example but you can go with the same way: stackoverflow.com/questions/6837002/…
  • do you know what the difference between using this method and using the AVPlayer method addPeriodicTimeObserver(TimeInterval:) ?
  • how to remove it please
  • From developer.apple.com/library/archive/releasenotes/Foundation/… In OS X 10.11 and iOS 9.0 NSNotificationCenter and NSDistributedNotificationCenter will no longer send notifications to registered observers that may be deallocated.
  • Passing the currentItem for the object Parameter was the important part - check that if you have problems.
  • wrong on two counts (1) you actually don't have to pass anything as an object (2) for years now, no need to deinit notifications
  • But it says [myClass playerDidFinishPlaying:]: unrecognized selector sent to instance
  • #selector(playerDidFinishPlaying)
  • does this addObserver induce memory leak when used multiple times?
  • Should I remove the observer? How do I do that?
  • 2019-4-20, XCode 10.2.1, Swift 5, Work for me, Thanks
  • nil is the magic word, or else you have to add/remove to every item you add👌
  • This is a fine idea and in practice it's often better to do this. With long material (songs etc) it's often better to catch it a second before the end (or less if you can), rather than deal with the total (undocumented) Apple mess when a file actually "ends".
  • does this addObserver induce memory leak when used multiple times?