3

嗨,我正在使用 AVPlayer 在我的 UITableViewCells 上播放视频,它在 iOS 7 上运行良好,但在 iOS8 中它因以下错误而崩溃。

       'An instance 0x7c01b000 of class AVPlayerItem was deallocated while key value observers were still registered with it.

这是我的代码

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
         {

          .........
          .........

        if(cell.videoPlayer!= nil && cell.videoPlayer.currentItem != nil)

           {
              [cell.videoItem removeObserver:self forKeyPath:@"playbackBufferEmpty" context:nil];
               [cell.videoItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp" context:nil];

               }
     cell.videoPlayer = [AVPlayer playerWithPlayerItem:cell.videoItem];
     cell.avLayer = [AVPlayerLayer playerLayerWithPlayer:cell.videoPlayer];
     cell.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

     [cell.videoItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionInitial context:nil];
     [cell.videoItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionInitial context:nil];

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidBufferPlaying:) name:AVPlayerItemPlaybackStalledNotification object:nil];

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

    cell.avLayer.frame = CGRectMake(5, 9, 310, 310);
   [cell.contentView.layer addSublayer:  cell.avLayer];
   [ cell.videoPlayer play];
   [cell.contentView addSubview:cell.videoActivity];


     return cell;
    }



   -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
               change:(NSDictionary *)change context:(void *)context {




      NSArray* cells = homeTabl.visibleCells;

      for (HomeCell* cell in cells) {

        if (object == cell.videoItem && [keyPath isEqualToString:@"playbackBufferEmpty"])                 {

   if (cell.videoItem.playbackBufferEmpty) {

        NSLog(@"buffering");
        cell.videoActivity.hidden = NO;



       }
    }

       else if (object == cell.videoItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"])
    {
         if (cell.videoItem.playbackLikelyToKeepUp)
        {


        cell.videoActivity.hidden = YES;
        [cell.videoPlayer play];

           }
        }

      }

    }


 -(void)scrollViewDidScroll:(UIScrollView *)aScrollView {

    NSArray* cells = homeTabl.visibleCells;

    for (HomeCell* cell in cells) {

    [cell.videoPlayer pause];
    [cell.avLayer removeFromSuperlayer];
    cell.videoPlayer = nil;
    cell.videoItem = nil;


   }

可能是什么原因 ?我已经完成了这个 SO question,但我无法在我的代码中实现它。请帮我解决这个问题。

4

1 回答 1

4

根据我的经验,删除观察者是 iOS 中最脆弱的领域之一。您需要非常小心地平衡对 AddObserver 和 RemoveObserver 的调用。我发现了一种故障安全的方法是把任何 AddObserver 调用放入对象的 Init 方法中,然后在其 Dealloc 方法中使用 RemoveObserver 调用来平衡这些调用。在您的情况下,这将在您的“videoItem”子类中。(此代码未检查)

- (id) initWithOwner:(id)owner
{
    self = [super init];
    if( self ) {
        _owner = owner;
        [self addObserver:_owner forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionInitial context:nil];
    }
    return self;
}

- (void) dealloc
{
    [self removeObserver:_owner forKeyPath:@"playbackBufferEmpty" context:nil];
}

我不确定 videoItem 是在哪里声明的,但基本上你创建了一个名为 VideoItem 的新类,并在其中创建了一个名为 initWithOwner: 的新初始化程序。在您的 cellForRowAtIndexPath: 方法中,当您创建新单元格时,您还创建一个 VideoItem 实例并将 self 作为所有者传递

self.videoItem = [[VideoItem alloc] initWithOwner:self];

如果没有更多您的代码,我无法真正更详细地指定这一点。您可能还考虑先在 xcode 中格式化您的代码,然后将其剪切并粘贴到 SO 中以使其更整洁。

于 2015-01-09T08:16:09.650 回答