1

Hi I want to play the video URL inside the UITableView when the cell is completely visible. How is it possible ?

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

     if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}



 //Play the movie now
NSURL *videoURL =[NSURL fileURLWithPath:myURL];
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
videoPlayerView.moviePlayer.fullscreen=TRUE;

[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
 return cell;

 }
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130

1 Answers1

0

You can prepare the videos on

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

In cellForRowAtIndexPath method check if the current cell is visible by compering the index path with

NSArray *visiblePaths = [tableView indexPathsForVisibleRows];

if yes play it if not stop playing.Depending on the videos and number of videos you can extend this approach with some caching mechanism of course.YOu can preload all the videos if you feel that loading videos on willDisplayCell makes a lag.

A helper function to find if the cell is visible or not.You can call this function in cellForRowatIndexpath.

- (BOOL)isIndexPathVisible:(NSIndexPath*)indexPath
{
NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];

for (NSIndexPath *currentIndex in visiblePaths)
{
    NSComparisonResult result = [currentIndex compare:currentIndex];

    if(result == NSOrderedSame)
    {
        NSLog(@"Visible");
        return YES;
    }
}

return NO;
}
Ilker Baltaci
  • 11,644
  • 6
  • 63
  • 79
  • I am not getting a correct idea, can you please explain more –  May 23 '14 at 12:40
  • I assume that you want to start playing movies as user scrolls the tableview like the new Facebook app?In this context you need to create a subclass of UITableViewCell with an embedded movie player.The code that you mentioned it the native movie player which is shown in full screen. – Ilker Baltaci May 23 '14 at 12:50
  • yea I will subclass that, I want the same thing, like instagram or vine. Please tell me how to print(NSLog) something in cellForRowatIndexpath when cell is fully visible –  May 23 '14 at 12:56
  • can you answer http://stackoverflow.com/questions/34767717/tableview-cell-video-autoplay ? – Aarti Oza Jan 19 '16 at 12:56