在集合视图单元格中显示视频

时间:2016-08-07 16:11:33

标签: uicollectionview avplayer avasset avplayerviewcontroller

我想在集合视图中显示多个视频。我有这段代码:

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return array.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    NSURL *videoURL = [NSURL fileURLWithPath:@"Path"];
    AVPlayer *player = [AVPlayer playerWithURL:videoURL];
    AVPlayerViewController *playerViewController = [AVPlayerViewController new];
    playerViewController.player = player;
    playerViewController.showsPlaybackControls = NO;
    playerViewController.videoGravity = AVLayerVideoGravityResizeAspectFill;

    playerViewController.view.frame = CGRectMake(0, 0, 200, 200);
    [cell insertSubview:playerViewController.view atIndex:0];

    [player play];

    return cell;

}

但代码非常慢,滚动不顺畅。你能帮助我吗?

1 个答案:

答案 0 :(得分:2)

是的,我的示例代码完全符合您的要求,甚至更多。您可以下载代码并观看视频,了解它的作用来自/ at:

http://demonicactivity.blogspot.com/2016/08/draft-what-in-hell-this-demoniac-is-up.html