我正在调整我的应用程序到 Swift 3,我遇到了这个问题.这曾经在Swift 2.2上运行,但现在已经坏了. moviePlayBackFinished永远不会被调用.我尝试以多种方式添加观察者,但都没有成功. NotificationC
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); }
任何想法都会受到关注.
谢谢
这个问题有多种可能的原因.NotificationCenter .default .addObserver(self, selector: #selector(self.moviePlayBackFinished(sender:)), name: .AVPlayerItemDidPlayToEndTime, object: player.currentItem)
首先,您必须将对象设置为nil. AVPlayerItemDidPlayToEndTime通知自动将对象设置为最后到达的AVPlayerItem.如果您手动更改它,您将不会收到任何AVPlayerItemDidPlayToEndTime的通知.
其次,根据AVPlayerItemDidPlayToEndTime文档:
Important
This notification may be posted on a different thread than the one on which the observer was registered.
因此,要确保您应该检查通知是否在您想要的主题上.
你从UIViewController或AVPlayer子类添加观察者并不重要.