对所有当前播放的视频执行一个AVPlayer的AVPlayerItemDidPlayToEndTime操作 [英] One AVPlayer's AVPlayerItemDidPlayToEndTime action executed for all Currently playing videos
问题描述
问题:在具有播放器的collectionview单元格中
Problem : In collectionview cell which has player
如果我同时播放两个视频并寻找第一个视频结束,然后AVPlayerItemDidPlayToEndTime
触发两次并且两个视频都重新启动
if I play two Video simultaneously and seek first Video to end then AVPlayerItemDidPlayToEndTime
fired for two times and both videos restarted
我在收藏夹视图单元格中
In collection view cell I have
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()
}
})
}
和一个播放按钮动作来播放视频.
在单元格中,我要寻找滑块.
and one play button action which play the video.
In cell I have slider to seek.
任何帮助将不胜感激
推荐答案
在注册通知时,请确保player
和player?.currentItem
不等于nil
.对我来说,似乎其中之一是nil,而您基本上是在订阅所有.AVPlayerItemDidPlayToEndTime
通知(因为object
是nil).
Make sure that player
and player?.currentItem
are not equal to nil
when you're registering for notifications. To me, it seems like one of them was nil and you're basically subscribing to all of the .AVPlayerItemDidPlayToEndTime
notifications (since object
is nil).
为避免这种情况,请在将AVAsset
分配给播放器后立即订阅通知.
To avoid that, subscribe to the notifications right after assigning AVAsset
to the player.
这篇关于对所有当前播放的视频执行一个AVPlayer的AVPlayerItemDidPlayToEndTime操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!