类AVPlayerItem被释放,而键值观察者仍然被注册

时间:2017-03-07 14:21:17

标签: uitableview pagination avplayeritem

我收到了以下错误,但奇怪的是,只有当我使用分页在我的tableview上滚动,加载第二组帖子,而不是我尝试移出新视图控制器, 如果我只加载帖子的第一页,并移出到另一个视图控制器,则没有崩溃:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'An instance 0x170212330 of class AVPlayerItem was deallocated while key 
value observers were still registered with it. 
Current observation info: <NSKeyValueObservationInfo 0x174820360> (
<NSKeyValueObservance 0x174244290: Observer: 0x101c76c00, Key path: 
playbackBufferEmpty, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, 
Property: 0x174243ea0>
<NSKeyValueObservance 0x1754583c0: Observer: 0x101c76c00, Key path: 
playbackLikelyToKeepUp, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, 
Property: 0x174243f60>

我的手机上有一个播放器,这是我的视图控制器代码:

- (UITableViewCell *)tableView:(UITableView *)tableViewSelected cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = @"postCell";
    PostCell* updateCell = [tableViewSelected dequeueReusableCellWithIdentifier:identifier];

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

        dispatch_async(queue, ^{

            dispatch_sync(dispatch_get_main_queue(), ^{
                NSLog(@"ADD VIDEO PLAYER and PLAY VIDEO");



                NSString* videoString = [NSString stringWithFormat:@"%@%@%@",UrlBase,PostVideos,post.video];
                NSString* expandedPath = [videoString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                 NSURL *videoURL = [NSURL URLWithString:expandedPath];
                NSLog(@"URL : %@",videoURL);


                updateCell.videoItem  = [AVPlayerItem playerItemWithURL:videoURL];

                [updateCell.videoItem addObserver:updateCell forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
                [updateCell.videoItem addObserver:updateCell forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];

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

                [[NSNotificationCenter defaultCenter] addObserver:updateCell selector:@selector(itemDidBufferPlaying:) name:AVPlayerItemPlaybackStalledNotification object:nil];
                [[NSNotificationCenter defaultCenter] addObserver:updateCell selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[updateCell.videoPlayer currentItem]];


                updateCell.avLayer.frame = updateCell.picture.bounds;
                [updateCell.videoView.layer addSublayer:updateCell.avLayer];

                [updateCell.avLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
                if(indexPath.row==0){
                    [updateCell.videoPlayer play];
                }
            });
        });
        return updateCell;

}

我在添加第二页时可以看到的唯一区别是:

在执行常规表视图加载时,我执行:

  [_tableView reloadData];

在同一个表格视图中加载第二页时,我会这样做:

 [_tableView beginUpdates];
 [_tableView insertRowsAtIndexPaths:[[GlobalSingleton sharedInstance] indexPathsToInsert]  withRowAnimation:UITableViewRowAnimationTop];
 [_tableView endUpdates];

比我的手机还要:

- (void)prepareForReuse{
    [self removePlayer];

}

- (void) dealloc {
[self removePlayer];
}

-(void)removePlayer{
    @try{

    [self.videoItem removeObserver:self forKeyPath:@"playbackBufferEmpty"];
    [self.videoItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp"];




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

        NSLog(@"remove Observer!");
    // [avLayer.player pause];
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
    }
    @finally {
        NSLog(@"finally");
        [self.avLayer removeFromSuperlayer];
        self.playIV.hidden = YES;
        self.videoActivity.hidden = YES;
        self.videoView.hidden = YES;
        self.videoItem = nil;
        self.avLayer = nil;
        self.videoPlayer = nil;
    }

}

insertRowsAtIndexPaths是否可能导致单元格永远不会被释放?我无法找到让观察员注册的可能性。

1 个答案:

答案 0 :(得分:0)

在使用insertRowsAtIndexPaths时,不会调用prepareForReuse,因此我在将新视频项添加到Cell之前,通过取消注册cellForRowAtIndexPath上的观察者来解决它:

- (UITableViewCell *)tableView:(UITableView *)tableViewSelected cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = @"postCell";
PostCell* updateCell = [tableViewSelected dequeueReusableCellWithIdentifier:identifier];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

    dispatch_async(queue, ^{

        dispatch_sync(dispatch_get_main_queue(), ^{
            NSString* videoString = [NSString stringWithFormat:@"%@%@%@",UrlBase,PostVideos,post.video];
            NSString* expandedPath = [videoString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             NSURL *videoURL = [NSURL URLWithString:expandedPath];

            /** SOLUTION STARTS HERE **/
            @try{
                  [self.videoItem removeObserver:self forKeyPath:@"playbackBufferEmpty"];
                  [self.videoItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp"];
                  [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemPlaybackStalledNotification object:nil];
                  [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

           }
           @catch (NSException * e) {
               NSLog(@"Exception: %@", e);
           }
           @finally {
               NSLog(@"finally");
           }
           /** SOLUTION ENDS HERE **/

            updateCell.videoItem  = [AVPlayerItem playerItemWithURL:videoURL];
            [updateCell.videoItem addObserver:updateCell forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
            [updateCell.videoItem addObserver:updateCell forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];

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

            [[NSNotificationCenter defaultCenter] addObserver:updateCell selector:@selector(itemDidBufferPlaying:) name:AVPlayerItemPlaybackStalledNotification object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:updateCell selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[updateCell.videoPlayer currentItem]];


            updateCell.avLayer.frame = updateCell.picture.bounds;
            [updateCell.videoView.layer addSublayer:updateCell.avLayer];

            [updateCell.avLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
            if(indexPath.row==0){
                [updateCell.videoPlayer play];
            }
        });
    });
    return updateCell;

}