Paste video from url into UITableViewCell with autorun, e.g. Twitter

as my question says, I want to embed a video in a UITableViewCell, for example, on Twitter. With autoplay without sound.

This is my setSelected method in VideoCell.m

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state NSURL *urlVideoFile = [NSURL URLWithString:self.urlVideo]; NSAssert(urlVideoFile, @"Expected not nil video url"); _playerViewController = [[AVPlayerViewController alloc] init]; _playerViewController.player = [AVPlayer playerWithURL:urlVideoFile]; _playerViewController.view.frame = self.viewVideo.bounds; _playerViewController.showsPlaybackControls = YES; [self.viewVideo addSubview:_playerViewController.view]; self.viewVideo.autoresizesSubviews = YES; _playerViewController.player.play; } 

But, obviously, this is not the best way to do it, because it is very slow to load videos and for consecuences, the table scroll is interrupted continuously!

I would like to know what is the best way to do this! Thank you

Sorry for my English!

EDIT:

I solved the loading problem, this problem blocks the scrolling of the table, with the dispatch_assync method.

I delete the code inside setSelected and put it in cellForRow in the main view controller

 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", [arrayVideos objectAtIndex:indexPath.row]]]; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); dispatch_async(queue, ^{ cell.videoItem = [AVPlayerItem playerItemWithURL:url]; dispatch_sync(dispatch_get_main_queue(), ^{ cell.videoPlayer = [AVPlayer playerWithPlayerItem:cell.videoItem]; cell.avLayer = [AVPlayerLayer playerLayerWithPlayer:cell.videoPlayer]; cell.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; cell.avLayer.frame = CGRectMake(cell.tapaVideo.frame.origin.x, cell.tapaVideo.frame.origin.y, cell.tapaVideo.frame.size.width, cell.tapaVideo.frame.size.height); [cell.contentView.layer addSublayer:cell.avLayer]; //[cell.videoPlayer play]; //[cell.contentView addSubview:cell.videoActivity]; }); }); 

But I still have a "overlap problem."

enter image description here

+5
source share

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


All Articles