I am working with the AVPlayer
in UITableView
just like facebook is autoplaying videos in the timeline.
Playing videos is working fine.
The issue is when I scroll the indexPath, I need to change the video as well using URL.
I am using below code to play the video in 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];
// }
});
});
How can I play the different videos in each cell? At the top of the cellForRowAtIndexPath:
i am using the below code for removing the player.
cell.picOrVideo.image=nil;
cell.playButtonOnVideo.image=nil;
cell.videoPlayer = nil;
cell.avLayer=nil;
What am I missing?