大家好,我遇到了 UIPageViewController 和通知的问题。
我有一个带有页面数组的页面 UIPageVewController,所以在这些页面中我有一个 AVplayer 循环播放,如下所示:
func loopVideo(videoPlayer:AVPlayer){
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil, queue: nil){
[weak videoPlayer] notification in
videoPlayer?.seek(to: kCMTimeZero)
videoPlayer?.play()
}
}
问题是当我通过滚动更改页面时,来自另一个页面的通知更改了我当前播放的 AVPlayer 视频。我在通知中打印了一个打印件,我可以看到调用其他页面通知。我不知道我必须做什么?
我尝试使用 NotificationCenter.default.removeObserver(self) 删除 viewDidDisappear 中的通知,但没有成功。
你能帮帮我吗?
谢谢
最佳答案
NotificationCenter.default.removeObserver(self)
在这里不起作用,因为您从未将自己添加为目标。
而是保留对您的通知的引用并将其删除。我认为它应该看起来像这样:
var notificationObserver:NSObjectProtocol?
func loopVideo(videoPlayer:AVPlayer){
self.notificationObserver = NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil, queue: nil){
[weak videoPlayer] notification in
videoPlayer?.seek(to: kCMTimeZero)
videoPlayer?.play()
}
}
func removeObserver() {
NotificationCenter.default.removeObserver(self.notificationObserver)
}
关于ios - 删除 UIPageViewController 中的循环 AVPlayer Observer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42252594/