我在设置时AVPlayer
在表格view.table
视图中使用滞后AVPlayerItem
。下面是cellForRowAtIndexPath
方法代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableIdentifier = @"tableIdentifier";
CustomTableCell *cell = [self.videoTableView dequeueReusableCellWithIdentifier:tableIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"VideoCell" owner:self options:nil]objectAtIndex:0];
}
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
AVAsset *avAssert = [AVAsset assetWithURL:[NSURL URLWithString:@"url"]];
cell.playerItem =[[AVPlayerItem alloc]initWithAsset:avAssert];
dispatch_sync(dispatch_get_main_queue(), ^{
if (!cell.avPlayer) {
cell.avPlayer = [AVPlayer playerWithPlayerItem:cell.playerItem];
}else{
[cell.avPlayer replaceCurrentItemWithPlayerItem:cell.playerItem];
}
if (!cell.avPlayerLayer) {
cell.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:cell.avPlayer];
cell.avPlayerLayer.frame = cell.videoPlayerView.layer.bounds;
cell.avPlayerLayer.videoGravity = AVLayerVideoGravityResize;
[cell.videoPlayerView.layer addSublayer: cell.avPlayerLayer];
if (indexPath.row == 0) {
cell.avPlayer.muted = NO;
[cell.avPlayer play];
}
}
});
});
return cell;
}
问题来了,
if (!cell.avPlayer) {
cell.avPlayer = [AVPlayer playerWithPlayerItem:cell.playerItem];
}else{
[cell.avPlayer replaceCurrentItemWithPlayerItem:cell.playerItem];
}
如果我在代码表视图上方评论可以正常工作而不会滞后,但我需要替换AVPlayerItem
.