Comment lire la video (autoplay) dans UITableViewCell dans iOS

Je joue une video dans UITableViewCell . Pour cela j'utilise le code suivant:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSSsortingng *VideoCellIdentifier = @"VideoCell"; NSDictionary *_response_data = [self.response objectAtIndex:indexPath.row]; VideoCustomCell *cell = (VideoCustomCell *) [tableView dequeueReusableCellWithIdentifier:VideoCellIdentifier]; if (cell == nil) { NSArray *topLevelObjects; topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"VideoCustomCell" owner:self options:nil]; for (id currentObject in topLevelObjects){ if ([currentObject isKindOfClass:[UITableViewCell class]]){ cell = (VideoCustomCell *) currentObject; cell.delegate = self; break; } } } avPlayer = [[AVPlayer playerWithURL:[NSURL URLWithSsortingng:[_response_data valueForKey:@"media_id"]]] retain]; avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer]; avPlayerLayer.frame = cell.video_player_view.layer.bounds; avPlayerLayer.videoGravity = AVLayerVideoGravityResize; [cell.video_player_view.layer addSublayer: avPlayerLayer]; [avPlayer play]; return cell; } 

La video est correctement lue, mais je ne souhaite lire qu'une seule video à la fois. Joue la video de la cellule qui est entièrement visible.

Utilisez cette méthode de défilement et de gérer la lecture video. Cette deux méthode appellera dans les deux cas lorsque le visionnage arrête le défilement

 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if(![scrollView isDecelerating] && ![scrollView isDragging]){ [self playVideo]; } } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ if(!decelerate){ [self playVideo]; } } -(void)playVideo { if(aryTableData.count==0){ return; } for(UITableViewCell *cell in [tblView visibleCells]) { VideoCell *ccell = (VideoCell*)cell; CGRect ccellRect = [APP_DEL.window convertRect:ccell.bounds fromView:ccell]; // NSLog(@"--Cell frame %f",ccellRect.origin.y); //Set Condition of cell visible within some range if(ccellRect.origin.y>-200) { // Handle Video Play int row = [[tblView indexPathForCell:ccell] row]; NSSsortingng *strUrl = [[aryTableData objectAtIndex:row] valueForKey:@"video_url"] ; [ccell startVideoFromURL:strUrl]; //You can handle video play in cell or table view } } } 

Vous devriez écrire votre code de jeu dans la méthode didselect pour que vous sélectionniez la ligne, elle ne jouera pas autre chose.

 (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // your player code }