當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]; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM