首页 > 解决方案 > 检测 AVQueuePlayer 中的视频项目何时更改

问题描述

在我的项目中,我使用 AVQueuePlayer 播放多个视频,在某些情况下,我想知道当前项目何时完成以及下一个项目何时开始。我像这样使用通知:

NotificationCenter.default.addObserver(self, selector: #selector(self.goToStepTwo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)

但此方法仅适用于第一项。

谢谢

标签: iosswift

解决方案


使用

.addObserver(self, selector: #selector(self.goToStepTwo), name: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)

您明确要求在self.player?.currentItem播放到最后时收到通知。

要收到任何项目的通知,只需nil用作object

.addObserver(self, selector: #selector(self.goToStepTwo), name: .AVPlayerItemDidPlayToEndTime, object: nil)

来自https://developer.apple.com/documentation/foundation/notificationcenter/1411723-addobserver

obj

观察者想要接收其通知的对象;也就是说,只有这个发送者发送的通知才会传递给观察者。

如果通过nil,通知中心不会使用通知的发送者来决定是否将其传递给观察者。


推荐阅读