如何在iOS中的UICollectionView中顺畅播放多个video?

我想在我的集合视图中以无限循环播放多个video。

每个video代表一个单元格。

我正在使用ALAsset。

我正在使用AVPLayer播放它,但它不能正常加载和播放。 有什么建议么。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; CGRect attachmentFrame = CGRectMake(2, 2, cell.frame.size.width-4, cell.frame.size.height-4); ALAsset *asset = self.assets[indexPath.row]; UIView* subView; if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) { // asset is a video avPlayer = [[AVPlayer alloc] initWithURL:[[asset defaultRepresentation] url]]; avPlayer.muted = YES; avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:avPlayer]; [avPlayerLayer setFrame:CGRectMake(0, 0, cell.frame.size.width-4, cell.frame.size.height-4)]; subView = [[UIView alloc]initWithFrame:attachmentFrame]; [subView.layer addSublayer:avPlayerLayer]; [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; [cell.contentView addSubview:subView]; [avPlayer seekToTime:kCMTimeZero]; avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; [avPlayer play]; avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[avPlayer currentItem]]; } } - (void)playerItemDidReachEnd:(NSNotification *)notification { AVPlayerItem *p = [notification object]; [p seekToTime:kCMTimeZero]; } 

我也尝试过MPMoviePlayerController但是使用电影播放器​​你只能循环播放一个video。 任何其他与缓冲video或缩略图相关的建议。 我不想在video中播放声音。

AVPlayer能够非常流畅地在应用程序中播放多个video,但您需要管理单元格以进行收集视图,因为在您的代码中,当您的单元格重新加载时,您的新AVPlayer对象会创建相同的video,从而产生问题。

因此,您需要实现这样一种机制,通过该机制可以实现为一个video创建单个AVPlayer对象。 一个建议是管理AVPlayer对象的池。

谢谢