-1

I want to display videos in UITableView like Instagram that will play automatically when the cell is visible and pause when cell disappears from screen. I tried everywhere to find solution but all I found is very complex code to even understand. At the moment two cell is displayed on screen. Only one video should play and all other should stop.

I also need to care about memory leaks. So I don't know where I can allocate AVPlayerViewController. I deal with array of URLs to play videos.

Please give me perfect solution in detail. Help will be appreciated.

EDIT:

This method gives index array which are displayed on the screen

-(BOOL)isRowVisible :(NSIndexPath*)indexPath {
    NSArray *indexes = [tblHome indexPathsForVisibleRows];
    for (NSIndexPath *index in indexes) {
        if (index.row == indexPath.row) {
            return YES;
        }
    }

    return NO;
}

In cellForRowAtIndexpath method-

BOOL visibleCell = [self isRowVisible:indexPath];
if(visibleCell == YES)
{
    //if(!avpController)
    {
        avpController = nil;
        avpController= [[AVPlayerViewController alloc] init];
        avpController.player=videoPlayer;
       // avpController.view.frame = CGRectMake(0, 50, SCREEN_WIDTH, 100); //CGRectMake(0, userDetailView.frame.origin.y + userDetailView.frame.size.height +5, SCREEN_WIDTH, 200) ;
        avpController.view.frame = videoView.bounds;

        [videoView addSubview:avpController.view];

    }

    NSURL *url= [NSURL URLWithString:[arrVideo valueForKey:@"video_path"]];
    videoPlayer = [AVPlayer playerWithURL:url];
    [videoPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
    [videoPlayer play];
}
Hiren Prajapati
  • 717
  • 3
  • 10
  • 25
  • could you please update ur question with what you tried and highlight the issue ? – Teja Nandamuri May 26 '16 at 13:03
  • @TejaNandamuri I have edited question for you. Please check it – Hiren Prajapati May 26 '16 at 13:12
  • FYI..you dont have to do if(visibleCell == YES), just do if(visibleCell)@Hiren – Teja Nandamuri May 26 '16 at 13:24
  • It doesn't matter even after changing that, things remain same – Hiren Prajapati May 26 '16 at 13:26
  • no i'm just saying that you can do that in simple way. Ofcourse that doesnt solve the problem. Hence I mentioned the word FYI(for your information) at the beginning of comment – Teja Nandamuri May 26 '16 at 13:27
  • As you said there will be only two cells visible on screen, and you have the indexes of those two cells. You can easily find out which index is lower from the two indexes and you can pplay the video in that cell @Hiren – Teja Nandamuri May 26 '16 at 13:28
  • yeah Thats what I am trying to solve now. Rather than getting BOOL value from isRowVisible(), I am getting top most indexpath and comparind that indexpath with current indexpath in cellForRowAtIndexpath. If success, then I create and play video. But How can I pause the video if cell goes out of screen? – Hiren Prajapati May 26 '16 at 13:37

2 Answers2

0

First you need a custom cell for your tableview, based on UITableViewCell. Within that you should have a method that creates an AVPlayer (also a class property) on a call, for eg :

- (void) createPlayerWithURL:(NSURL)url {
    AVAsset *asset = [AVAsset assetWithURL:url];
    self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
    self.player = [[AVPlayer alloc] initWithPlayerItem:self.playerItem];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    [self.playerLayer setFrame:self.view.layer.bounds];
    [self.view.layer addSublayer:self.playerLayer];
    [self.player play];
}

Add a link to that method in the custom cell header file, then call it from cellForRowAtIndexPath, passing in the URL for your video.

Theres another method you can use to check when cell goes out of view : tableView:didEndDisplayingCell:forRowAtIndexPath: . Use that method on the tableview to get the cell object - the you can simply call to stop the player, ie [cell.player pause];

Luke Smith
  • 1,218
  • 12
  • 17
0

You should make use of the - (void)scrollViewDidScroll:(UIScrollView *)scrollView (or other delegate methods associated with the scrollview) to track the movement of the tableview. You should be then able to tell when the Cell has moved out of the screen.

Xcoder
  • 1,762
  • 13
  • 17