微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AVPlayerItemDidPlayToEndTime永远不会到达Swift 3上的选择器

我正在调整我的应用程序到 Swift 3,我遇到了这个问题.这曾经在Swift 2.2上运行,但现在已经坏了. moviePlayBackFinished永远不会被调用.我尝试以多种方式添加观察者,但都没有成功.

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

    NotificationCenter.default.addobserver(self,selector: #selector(moviePlayBackFinished(_:)),selector: #selector(self.moviePlayBackFinished(sender:)),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,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子类添加观察者并不重要.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐