dispatch_sync(dispatch_get_main_queue(), ^{
if (cell.videoLayer) {
[cell.videoLayer removeFromSuperlayer];
}
cell.videoPlayer = [AVPlayer playerWithPlayerItem:cell.videoItem];
cell.videoLayer = [AVPlayerLayer playerLayerWithPlayer:cell.videoPlayer];
cell.videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
cell.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
cell.videoLayer.frame = cell.videoPlayerContainer.bounds;
[cell.videoPlayerContainer.layer addSublayer:cell.videoLayer];
cell.videoPlayer.volume = 0.0;
NSLog(@"Video Play at index: %ld", (long)indexPath.row);
[cell.videoItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionInitial context:nil];
[cell.videoItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionInitial context:nil];
[cell.videoPlayer addObserver:self forKeyPath:@"currentItem.status" options:NSKeyValueObservingOptionNew context:kStatusDidChangeKVO];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[cell.videoPlayer currentItem]];
[cell.videoPlayer play];
});
这是我的cellForItemAtIndexPath代码