How to play video (auto play) in UITableViewCell in iOS

I am playing a video in a UITableViewCell . For this, I use the following code:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *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 URLWithString:[_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; } 

The video plays correctly, but I want to play only one video at a time. Play the video of the fully visible cell.

+6
source share
2 answers

Use two methods for scrolling and playing videos. These two methods will be called in any case, when viewing the stop list will scroll

 -(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]; NSString *strUrl = [[aryTableData objectAtIndex:row] valueForKey:@"video_url"] ; [ccell startVideoFromURL:strUrl]; //You can handle video play in cell or table view } } } 
+3
source

You must write your replay code in the didselect method, since you select a line, it will play only others.

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

Source: https://habr.com/ru/post/954835/


All Articles