首页 文章

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

提问于
浏览
0

我将一个名为' status '的 observer 添加到 AVPlayerItem 中 . 发送观察者,然后在观察者未完成时将AVPlayerItem设置为nil

我在dealloc AVPlayerItem时删除了观察者

收到以下错误:

NSInternalInconsistencyException',原因:'AVPlayerItem类的实例0x7dc5e7d0被释放,而键值观察者仍然注册了它 . 当前观察信息:(上下文:0x0,属性:0x7b8ad140>

3 回答

  • 0

    我不相信 AVPlayerItem 应该观察任何事情,如果没有具体的例子,你很难说 . 通常,此流程将是 controller 是来自 AVPlayerItem 的某些通知的观察者 .

    例如:

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

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

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

    如果您提供更多细节,也许我可以提供更多帮助 . 谢谢!

    Edit:

    很快就会......

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

    在将playerItem设置为nil之前,请删除观察者:

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

    如果你等到deinit / dealloc,在你已经将playerItem设置为nil之后,那么你将不再有对它的引用来移除观察者 .

  • 1

    它通过异步引起了avplayeritem的初始化

相关问题