销毁AVPlayer的实例

时间:2015-08-22 01:21:58

标签: ios objective-c uitableview avplayer

我有一个标准UITableView,其中包含自定义HeaderView

在tableview的自定义HeaderView中,我有2个按钮。

@interface HeaderView : UIView

UIButton *buttonforDataFeed1;
UIButton *buttonforDataFeed2; 

当按下每个按钮时,它会使用来自不同API的数据重新加载tableview。

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

}

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

}

一个数据Feed可以提取视频。另一个只是拉弦。当我按下视频按钮时,视频会显示在每个单元格的tableview中。当我按下我的字符串按钮时,它们也会出现。

问题是视频不会死亡,当点击另一个按钮时,它们会保持活跃并在屏幕上聚集在一起。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    static NSString *CellIdentifier = @"Cell";
    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];
        [cell setDelegate:self];

        //problem - i can create videos here but i can't destroy them later
        [cell setVideo:vid];
        [cell play];
        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;
    }

}

如何销毁AVPlayer的所有这些实例?作为参考,这是自定义VideoCell

@interface VideoCell : UITableViewCell

@property (nonatomic, strong, readonly) AVPlayer *videoPlayer;
@property (nonatomic, strong) AVPlayerLayer *videoLayer;

和setVideo函数

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

    //[self generateImageFromAsset:video];
    //[self.videoView addSubview:self.placeholder];

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

}

0 个答案:

没有答案