如何在UICollectionView中的iOS中流畅地播放多个视频? [英] How to play multiple videos smoothly in iOS in UICollectionView?

查看:449
本文介绍了如何在UICollectionView中的iOS中流畅地播放多个视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在收藏视图中无限循环播放多个视频.

I want to play multiple videos in infinite loop in my collection view.

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

Each Video represent a cell.

我正在使用ALAsset.

I am using ALAsset.

我正在使用AVPLayer播放此文件,但加载和播放均不流畅. 任何建议.

I am playing this using AVPLayer but it's not loading and playing smoothly. Any Suggestions.

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

I also tried MPMoviePlayerController but using movie player you can play only one video in loop. Any other suggestion related to buffered video or thumbnail images. I don't want to play sound in video.

推荐答案

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

AVPlayer able to play multiple video in app very smoothly but you need to manage cell for collection view because In your code, when your cell reload, your newAVPlayer object create for same video which create issue.

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

So you need to implement such an mechanism by which can achieve to create single AVPlayer object for one video. One suggestion is managing pool for AVPlayer objects.

谢谢

这篇关于如何在UICollectionView中的iOS中流畅地播放多个视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆