问题:在具有播放器的collectionview单元格中
如果我同时播放两个视频,然后寻找第一个视频结束,然后AVPlayerItemDidPlayToEndTime
被解雇两次并重启两个视频
在集合视图单元格中我有
override func awakeFromNib() {
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player?.currentItem, queue: .main, using: {[weak self] (notification) in
if self?.player != nil {
self?.player?.seek(to: kCMTimeZero)
self?.player?.play()
}
})
}
播放视频的播放按钮动作 在单元格中,我有要查找的滑块。
任何帮助将不胜感激
答案 0 :(得分:3)
当您注册通知时,请确保player
和player?.currentItem
不等于nil
。对我而言,似乎其中一个是零并且您基本上订阅了所有.AVPlayerItemDidPlayToEndTime
通知(因为object
为零)。
为避免这种情况,请在将AVAsset
分配给播放器后立即订阅通知。
答案 1 :(得分:0)
Swift 5.1
将商品作为对象传递
// Stored property
let player = AVPlayer(url: videoUrl)
// When you are adding your video layer
let playerLayer = AVPlayerLayer(player: player)
NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: .AVPlayerItemDidPlayToEndTime, object: player.currentItem)
// add layer to view
然后,当您收到该通知时,以下是获取currentItem
的方法:
// Grab the item from the notification object and ensure its the same item as the current players item
if let item = notification.object as? AVPlayerItem,
let currentItem = player.currentItem,
item == currentItem {
NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: nil)
// remove player from view or do whatever you need to do here
}