我有一个UITableViewController,标题中有2个按钮。
按钮1 - 从用户名API中提取数据,刷新表格视图,并在每个单元格中显示一个字符串用户名
按钮2 - 从视频API中提取数据,刷新tableview,并在每个单元格中显示一个AVPlayer实例。
当我按下视频按钮时,每个单元格都能很好地显示视频。
当我按下用户名按钮时,我可以看到下方的用户名表视图,但最重要的是所有拒绝销毁的视频。我需要找到一种方法来摧毁它们。
任何想法如何破坏AVPlayer的几个实例,每行一个tableview,立即?
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];
}
自定义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];
}