title: XCode - Getting videos to play in tableview cells like Vine & Instagram
body: <p>I'm trying to get a vine type video feed working in a tableview. I want the videos to play one at a time inside the tableview cell just like Vine and Instagram. </p>
  
  <p>Does anyone know how to do this? I'm using MPMoviePlayer.</p>
  
  <p>Thanks</p>
  
  <p>---- UPDATE ----</p>
  
  <p>I've got the auto-play call-to-action working well (please see 'Play this one') when each table cell is in the center but I'm having trouble getting multiple MPMoviePlayers to load/stop/play.</p>
  
  <p>I only require one video to play in its cell at any one time but I can only seem to get the video in the first cell playing and it is not responding to the Stop call (please see 'Don't play this one' ).</p>
  
  <p>Below is a snippet of my script. The only part not working is getting the videos to actually play and stop. The call to play and stop works fine. </p>
  
  <p>I have this in my CellForRow: </p>
  
  <pre><code>MPMoviePlayerViewController *videoPlayer;
  
  [videoPlayer.view removeFromSuperview];
  
  videoPlayer = [[MPMoviePlayerViewController alloc] init];
  
  
  int videoTag = 500+indexPath.row;
  
  [videoPlayer.view setTag:videoTag];
  
  videoPlayer.moviePlayer.repeatMode = MPMovieRepeatModeOne;
  
  videoPlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
  
  videoPlayer.view.frame = CGRectMake( 0, 0, 320, 320);
  
  [videoPlayer.view setBackgroundColor:[UIColor whiteColor]];
  
  [playerView addSubview:videoPlayer.view];
  </code></pre>
  
  <p>And I do this for calling the correct cell in the center of the view:</p>
  
  <pre><code>- (void)scrollViewDidScroll:(UIScrollView *)theView{
  
  UITableViewCell *cell = (UITableViewCell *) theView;
  
  
  CGPoint currentOffset = theView.contentOffset;
  
  
  for (UITableViewCell *cell in [videoTableView visibleCells]) {
  
      NSIndexPath *indexPath = [videoTableView indexPathForCell:cell];
  
      int videoTag = 500+indexPath.row;
  
      MPMoviePlayerViewController *videoPlayer = (MPMoviePlayerViewController *)[cell viewWithTag:videoTag];
  
      UILabel *test_label = (UILabel *)[cell viewWithTag:300];
  
      CGRect cellRect = [videoTableView convertRect:cell.bounds fromView:cell];
  
  
  
      int scrollPosition = currentOffset.y + 64; // 64px is the navigation bar height
  
      int cellPosition = cellRect.origin.y;
  
  
  
      if( scrollPosition &gt; cellPosition-100 &amp;&amp; scrollPosition &lt; cellPosition+100 )
      {
         test_label.text = [NSString stringWithFormat:@"Play this one" ];
  
  
  
          UIView *blankOut = (UIView *)[cell viewWithTag:200];
  
          UIView *playerView = (UIView *)[cell viewWithTag:201];
  
          UIImageView *videoCoverUp = (UIImageView *)[cell viewWithTag:202];
  
  
          [playerView.layer setCornerRadius:138];
          [playerView.layer setMasksToBounds:YES];
  
  
          if(_isPlaying!=true){
  
              NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
              NSString *documentsDirectory = [paths objectAtIndex:0];
              NSString *path = [documentsDirectory stringByAppendingPathComponent:@"video.mp4"];
  
              NSData *video_data = _tableObjects[indexPath.row][9];// Where my video data is stored
  
              [video_data writeToFile:path atomically:YES];
  
              NSURL *moveUrl = [NSURL fileURLWithPath:path];
  
  
              videoPlayer.moviePlayer.contentURL = moveUrl;
  
              [videoPlayer.moviePlayer prepareToPlay];
  
              [videoPlayer.moviePlayer play];
  
  
              _isPlaying = true;
  
              // Needed
              [[UIApplication sharedApplication] setStatusBarHidden:NO];
              [self setNeedsStatusBarAppearanceUpdate];
  
          }
  
  
  
  
      } else {
  
          test_label.text = [NSString stringWithFormat:@"Don't play this one" ];
  
          [videoPlayer.moviePlayer stop];
          [videoPlayer.moviePlayer setContentURL:[NSURL URLWithString:nil]];
          [videoPlayer.moviePlayer prepareToPlay];
  
          //[_videoPlayer.moviePlayer.view removeFromSuperview];
  
          _isPlaying = false;
  
      }
  
  
  }
  }
  </code></pre>