如何在观察者未完成时将AVPlayerItem设置为nil



我在AVPlayerItem中添加了一个名为"状态"的观察者。当观察者未完成时,发送观察者,然后将AVPlayerItem设置为nil

我已经在解除锁定AVPlayerItem 时删除了观察者

获得以下错误:

NSInternalConsistencyException",原因:"的实例0x7dc5e7d0类AVPlayerItem已解除分配,而键值观察程序仍在注册。当前观察信息:(上下文:0x0,属性:0x7b8ad140>

我认为AVPlayerItem不应该观察到任何东西,在没有具体例子的情况下,很难说。通常情况下,您的控制器是来自AVPlayerItem的一些通知的观察员。

例如:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(movieItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:nil];

然后,当您完成时(即,当您将AVPlayerItem设置为nil时),您将删除观察器:

[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

如果你提供更多的细节,也许我可以提供更多的帮助。谢谢

编辑:

在swift中它将是…

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "movieDidReachEnd", name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)

在将playerItem设置为nil之前,请删除observer:

playerItem.removeObserver(self, forKeyPath: "status")

如果在将playerItem设置为nil之后,等到deinit/dealloc,则将不再有对它的引用来删除观察者。

它导致异步初始化avplayeritem

最新更新