All*_*aga 0 xcode ios swift swift3
我需要把一个玩家放在循环中,但为什么当我添加
NotificationCenter.default.addObserver(forName:NSNotification.Name.AVPlayerItemDidPlayToEndTime,object: nil, queue:nil){
notification in
videoPlayer.seek(to: KCMTimeZero)
videoplayer.play()
}
}
Run Code Online (Sandbox Code Playgroud)
当我在viewController中给出dismiss时,我的视图留在内存中.我是如何重现视频的,每次打开ViewController时,我的内存都会增加
没有这个代码,它就会被删除.
我不知道自己要做什么
你能帮我吗?
您的代码有三个问题:
默认情况下,引用作为strong传递到块中.为确保不保留它们,请使用weak或unowned:
NotificationCenter.default.addObserver(forName:NSNotification.Name.AVPlayerItemdidPlayToEndTime,object: nil, queue:nil){
[weak videoPlayer] notification in
videoPlayer?.seek(to: KCMTimeZero)
videoplayer?.play()
}
Run Code Online (Sandbox Code Playgroud)从iOS 9开始,NotificationCenter 除非您使用块观察器(您是),否则不需要从观察者中删除观察者.您应该将引用存储到从NotificationCenter.addObserver:forName:object:queue:usingBlock:以下位置返回的观察者:
self.observer = NotificationCenter.default.addObserver(...)
Run Code Online (Sandbox Code Playgroud)并在viewWillDissappear:
NotificationCenter.default.removeObserver(self.observer)
Run Code Online (Sandbox Code Playgroud)
(或者,您可以使用选择器,如Chan Jing Hong指出的那样;在这种情况下,不再需要删除观察者,但可能需要根据您的应用程序的逻辑)
NSNotification.Name.AVPlayerItemdidPlayToEndTime,任何 播放AVPlayerItem到达结束时都会通知您.为了避免潜在的问题,听取他们对当前播放的项目的通知(通过更换object:nil用object: playerItem)