如何在iOS中的UICollectionView中流畅播放多个视频?

2024-01-01

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

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

我正在使用 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对象。

Thanks

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在iOS中的UICollectionView中流畅播放多个视频? 的相关文章

随机推荐