2

我有一个 UITableViewController 在标题中有 2 个按钮。

按钮 1 - 从用户名 API 中提取数据,刷新表格视图,并为每个单元格显示一个字符串用户名

按钮 2 - 从视频 API 中提取数据,刷新 tableview,并为每个单元格显示一个 AVPlayer 实例。

当我按下视频按钮时,每个单元格都会很好地显示一个视频。

当我按下用户名按钮时,我可以在下面看到用户名tableview,但在它上面是所有拒绝销毁的视频。我需要找到一种方法来摧毁它们。

任何想法如何立即销毁 AVPlayer 的多个实例,每行一个表视图?

UITableViewController:PFQueryTableViewController

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
        static NSString *CellIdentifier = @"Cell";

//the userOrVideoString is a string property assigned a value whenever the user presses the user or video button
        if([self.userOrVideoString isEqualToString:@"video"]) {
            VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[VideoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
            NSString *dateString = [NSDateFormatter localizedStringFromDate:object.updatedAt
                                                                  dateStyle:NSDateFormatterShortStyle
                                                                  timeStyle:NSDateFormatterFullStyle];

            PFFile *file = [object objectForKey:@"videoFile"];
            Video *vid = [Video videoWithStringURL:file.url];

            //problem - easy to init these videos, can't delete them though? 
            [cell setVideo:vid];
            [cell play];
            cell.textLabel.text = dateString;
            return cell;
        }
        else {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
            NSString *username = [object objectForKey:@"username"];
            cell.textLabel.text = username ;
            return cell;
        }

    }


-(void) didPressUserButton {
    self.userOrVideoString = @"User";
    [self.tableView reloadData];

    [self loadObjects];

}

-(void) didPressVideoButton {
    self.userOrVideoString = @"video";
    [self.tableView reloadData];
    [self loadObjects];

}

自定义视频单元:UITableViewCell

- (void)setVideo:(Video *)video
{
    NSLog(@"%s : %@", __PRETTY_FUNCTION__, video);

   //videoPlayer is an AVPlayer as a property 
    [self.videoPlayer replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithAsset:video.video]];
    [self.videoPlayer setActionAtItemEnd:AVPlayerActionAtItemEndNone];
    self.videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
    [self.videoLayer setPlayer:self.videoPlayer];
    [self.videoLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
    [self.videoView.layer addSublayer:self.videoLayer];
    self.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[self.videoPlayer currentItem]];

    // Here we add an KVO to the player to know when the video is ready to play
    // This is important if you want the user to see something while the video is being loaded
    [self.videoPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];

    // Here we need to add the indicator to the video views layer
    // Then start animating
    // But if the video is already ready to play, then we dont add it
    // This case comes up when the video has been cached like we have done so
    // and the cell is being reused
    if ([self.videoPlayer status] != AVPlayerStatusReadyToPlay) {
        [self.videoView.layer addSublayer:[self.indicator layer]];
        [self.indicator startAnimating];
    }

}

- (void)play
{
    [self.videoPlayer play];
}
4

0 回答 0