在UITableViewCell中播放video的最佳方式是什么

我正在尝试根据单元格位置在UITableViewCell中制作自动播放video。

我正在使用AVPlayer

这是我的代码:

__weak typeof(self)this = self; NSString* videoPath = @"http://test.com/test.mp4"; AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:videoPath] options:nil]; NSArray* keys = [NSArray arrayWithObjects:@"playable",nil]; [asset loadValuesAsynchronouslyForKeys:keys completionHandler:^(){ AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset]; this.avPlayer = [AVPlayer playerWithPlayerItem:playerItem]; this.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:this.avPlayer]; this.avPlayerLayer.frame = _videoContent.frame; dispatch_async(dispatch_get_main_queue(),^{ [this.videoContent.layer addSublayer:this.avPlayerLayer]; [this.avPlayer play]; }); }]; 

但是当我滚动表格时,我的UITableView被冻结了。 我认为有很多耗时的工作,但最重要的是

 [this.avPlayer play] 

所以我的问题是AVPlayer是这种情况下最好的方法吗? 有没有办法改善性能?

使用以下链接,这适合您的问题。

https://github.com/PRX/PRXPlayer

你确定创建AVPlayerItemAVPlayerAVPlayerLayer都可以在主线程上执行吗? 您可能想尝试将那些放在主队列中调度的块中。