2015-07-10 99 views
4

我想在我的收藏視圖中無限循環播放多個視頻。如何在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,但使用電影播放器​​,你只能播放一個視頻循環。任何其他有關緩衝視頻或縮略圖的建議。 我不想在視頻中播放聲音。

回答

0

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