当AVPlayer在被释放之后,Player一直监听的时间没有被移除,提示错误的解决办法

Xcode Consolu打印出来的提示:

An instance 0x156608c0 of class AVPlayer was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:

<NSKeyValueObservationInfo 0x15634320> (

<NSKeyValueObservance 0x15597fa0: Observer: 0x15674660, Key path: currentItem, Options: <New: YES, Old: NO, Prior: NO> Context: 0x134cc4, Property: 0x1558ff20>

<NSKeyValueObservance 0x15598da0: Observer: 0x15674660, Key path: rate, Options: <New: YES, Old: NO, Prior: NO> Context: 0x134cc0, Property: 0x1559ad40>

 

提示的说明了AVPlayer这个类已经被释放了,当已经登记了消息监听还登记着,这可能会引起消息的泄漏,我的解决办法是在移除视图之前取消监听时间.

AVPlayer的监听部分代码:

  [self setPlayer:[AVPlayer playerWithPlayerItem:self.playerItem]];
        
        /* Observe the AVPlayer "currentItem" property to find out when any
         AVPlayer replaceCurrentItemWithPlayerItem: replacement will/did
         occur.*/
        [self.player addObserver:self
                      forKeyPath:kCurrentItemKey
                         options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
                         context:AVARLDelegateDemoViewControllerCurrentItemObservationContext];
        
        /* Observe the AVPlayer "rate" property to update the scrubber control. */
        [self.player addObserver:self
                      forKeyPath:kRateKey    
                         options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
                         context:AVARLDelegateDemoViewControllerRateObservationContext];

 [self.playerItem addObserver:self
  
              forKeyPath:kStatusKey
                options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
 
  
                          context:AVARLDelegateDemoViewControllerStatusObservationContext];
 

解决办法:

- (void)viewWillDisappear:(BOOL)animated{
    NSLog(@"Disappear");
  if(
self.playerItem && self.player){
    [self.playerItem removeObserver:self forKeyPath:kStatusKey context:AVARLDelegateDemoViewControllerStatusObservationContext];
    
   [self.player removeObserver:self forKeyPath:kRateKey context:AVARLDelegateDemoViewControllerRateObservationContext];
     [self.player removeObserver:self forKeyPath:kCurrentItemKey context:AVARLDelegateDemoViewControllerCurrentItemObservationContext];
}

 

转载于:https://www.cnblogs.com/zuopeng/p/4233161.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值