如何在UICollectionView的iOS中顺利播放多个视频?

问题描述:

我想在我的收藏视图中无限循环播放多个视频。如何在UICollectionView的iOS中顺利播放多个视频?

每个视频代表一个单元格。

我正在使用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,但使用电影播放器​​,你只能播放一个视频循环。任何其他有关缓冲视频或缩略图的建议。 我不想在视频中播放声音。

AVPlayer能够在应用程序播放多个视频非常流畅,但你需要收集视图管理单元,因为在你的代码,当你的电池重装,新AVPlayer对象创建它创造的问题相同的视频。

所以你需要实现这样一种机制,通过它可以实现为一个视频创建单个AVPlayer对象。一个建议是管理池的AVPlayer对象。

谢谢

+0

我可以管理集合视图单元格。但是,你能给出任何'AVPlay'的例子。谢谢 –

+0

是AVPlay还是AVPlayer? –

+0

您是否需要示范代码来演示如何使用AVplayer播放视频?如果是的话,你可以轻松找到。如http://www.techotopia.com/index.php/IOS_8_Video_Playback_using_AVPlayer_and_AVPlayerViewController – Hindu