1

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?

Till
  • 27,559
  • 13
  • 88
  • 122
harish sami
  • 465
  • 4
  • 9
  • i think its not a good idea to play view in cellForRowAtIndexPath because its use more memory . and when your tableview reload at that time you face problem . – Himanshu Moradiya Jan 16 '17 at 12:49
  • @HimanshuMoradiya i am also facing same issue any solution.Tableview Reload I will handle that issue.Other wise any other solution – Bittoo Jan 17 '17 at 04:16
  • one tableviewcell didselectedindexpath deleagates method just open a new view and play video best solution. – Himanshu Moradiya Jan 17 '17 at 04:33
  • http://stackoverflow.com/a/39850431/6656894 refer this answer for your solution in this answer he want to change only play and stop button image and play audio file and now you can do same thing but forgot button icon and just play video in new controller if you want some example then i can help you . – Himanshu Moradiya Jan 17 '17 at 04:44
  • http://stackoverflow.com/a/42029030/4549304 Can you check this, if it helps? – Saurabh Yadav Feb 03 '17 at 16:46

0 Answers0