我正在使用AVPlayer
中的UITableView
,就像Facebook在时间轴中自动播放视频一样。
播放视频效果很好。
问题是当我滚动indexPath时,我还需要使用URL更改视频。
我使用以下代码在cellForRowAtIndexPath:
NSURL *videoURL=[NSURL URLWithString:[[self.lifeLineAllPostArray objectAtIndex:indexPath.row] objectForKey:@"video_url"]];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
cell.videoItem = [AVPlayerItem playerItemWithURL:videoURL];
dispatch_sync(dispatch_get_main_queue(), ^{
cell.videoPlayer = [AVPlayer playerWithPlayerItem:cell.videoItem];
cell.avLayer = [AVPlayerLayer playerLayerWithPlayer:cell.videoPlayer];
cell.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
// cell.avLayer.frame = CGRectMake(5, 9, 310, 310);
// cell.avLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, 250);
cell.avLayer.frame=cell.picOrVideo.bounds;
cell.avLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[cell.picOrVideo.layer addSublayer:cell.avLayer];
[cell.videoPlayer play];
[cell.videoPlayer setVolume:1.0];
// }
});
});
如何在每个单元格中播放不同的视频?在cellForRowAtIndexPath:
的顶部,我使用以下代码删除播放器。
cell.picOrVideo.image=nil;
cell.playButtonOnVideo.image=nil;
cell.videoPlayer = nil;
cell.avLayer=nil;
我错过了什么?