Destroying AVPlayer instances in a UITableView

844 views Asked by At

I have a UITableViewController with 2 buttons in the header.

Button 1 - pulls data from a username API, refreshes the tableview, and displays one string username per cell

Button 2 - pulls data from a video API, refreshes tableview, and displays one instance of AVPlayer per cell.

When I press the video button, each cell nicely displays a video.

When I press the username button, I can see the username tableview underneath, but on top of it are all the videos that refuse to be destroyed. I need to find a way to destroy them.

Any ideas how to destroy several instance of an AVPlayer, one per row of a tableview, instantly?

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

}

Custom VideoCell : 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];
}
0

There are 0 answers