Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayerItemDidPlayToEndTime never reaches selector on Swift 3

I'm adapting my app to Swift 3 and I'm encountering this problem. This used to work on Swift 2.2 but now it's broken. moviePlayBackFinished never gets called. I tried adding the observer in multiple ways, all without success.

    NotificationCenter.default.addObserver(self, selector: #selector(self.moviePlayBackFinished(_:)),
                                           name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem);

    NotificationCenter.default.addObserver(self, selector: #selector(moviePlayBackFinished(_:)),
                                           name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem);

    NotificationCenter.default.addObserver(self, selector: #selector(self.moviePlayBackFinished(sender:)),
                                           name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem);

// (...)

@objc func moviePlayBackFinished(sender : AnyObject) {
    print("playbackFinished");
    let zeroCM : CMTime = CMTime(seconds: 0, preferredTimescale: 1000000000);
    playerLayer.player?.seek(to: zeroCM);
}

@objc func moviePlayBackFinished(_ notification: Notification) {
    print("playbackFinished");
    let zeroCM : CMTime = CMTime(seconds: 0, preferredTimescale: 1000000000);
    playerLayer.player?.seek(to: zeroCM);
}

Any ideas would be appriciated.

Thank you

like image 985
Jonathan Bursztyn Avatar asked Sep 23 '16 20:09

Jonathan Bursztyn


2 Answers

There are multiple possible reason of this issue.

NotificationCenter
    .default
    .addObserver(self, 
                 selector: #selector(self.moviePlayBackFinished(sender:)),
                 name: .AVPlayerItemDidPlayToEndTime,
                 object: player.currentItem)

First of all, you have to set object to nil. AVPlayerItemDidPlayToEndTime notification automatically set object to the AVPlayerItem which reached at the end. If you change it manually, you will not get any notification of AVPlayerItemDidPlayToEndTime.

Second, according to the AVPlayerItemDidPlayToEndTime documentation:

Important

This notification may be posted on a different thread than the one on which the observer was registered.

So, to make sure you should check the notification is on the thread you want.

It doens't matter you added observer from UIViewController or AVPlayer subclass.

like image 146
Ryan Avatar answered Sep 28 '22 00:09

Ryan


use this code..

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

playw is function name you have to make function like

func playw(){
//do here whatever you want to do

    }

Still you've any problem you can ask me.

like image 25
JAck Avatar answered Sep 28 '22 00:09

JAck