繁体   English   中英

对所有当前播放的视频执行一个 AVPlayer 的 AVPlayerItemDidPlayToEndTime 操作

[英]One AVPlayer's AVPlayerItemDidPlayToEndTime action executed for all Currently playing videos

问题:在有播放器的 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()
            }
        })
   }

和一个播放视频的播放按钮动作。
在单元格中,我有滑块要查找。

在此处输入图像描述

任何帮助,将不胜感激

注册通知时,请确保playerplayer?.currentItem不等于nil 对我来说,似乎其中一个是零,你基本上订阅了所有.AVPlayerItemDidPlayToEndTime通知(因为object是零)。

为避免这种情况,请在将AVAsset分配给播放器后立即订阅通知。

斯威夫特 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
}     

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM