zoukankan      html  css  js  c++  java
  • 当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];
    }
  • 相关阅读:
    .NET下的多线程编程—1线程机制概述
    C#温故而知新学习系列之面向对象编程—定义类与创建类的对象(一)
    C#温故而知新学习系列之面向对象编程—方法(四)
    C#温故而知新学习系列之XML编程—XmlSerializer类把复杂对象序列化为XML文档(六)
    深度剖析ASP.NET架构—ASP.NET请求的处理过程(一)
    ref参数
    Android实现开机自动运行程序(转)
    ASP.NET中的状态—基于服务器端的状态管理Session(二)
    深度剖析ASP.NET架构—HttpHandler(三)
    .NET下的多线程编程—2前台线程和后台线程
  • 原文地址:https://www.cnblogs.com/zuopeng/p/4233161.html
Copyright © 2011-2022 走看看