AVPlayer实例可以同时播放两个视频吗?

发布于 2024-11-17 09:03:06 字数 295 浏览 1 评论 0原文

我目前正在使用 MPMoviePlayerController 在我的应用中播放视频。当我从一个视频切换到下一个视频时,这不是一个非常平滑的过渡。我得出的结论是,我需要做的是淡出第一个视频,同时淡入第二个视频。为此,我需要同时播放两个视频。我知道我无法使用 MPMoviePlayerController 做到这一点。它在文档中。可以用AVPlayer来完成吗?我可以有两个 AVPlayer 实例,播放不同的电影,并在我的应用程序中同时播放,并且用户可以看到两部电影吗?

I'm currently using MPMoviePlayerController to play videos in my app. When I switch from one video to the next, it's not a very smooth transition. I've come to the conclusion that what I need to do is fade the first video out while fading the second video in. To do that I'll need to have two videos playing simultaneously. I know I can't do that with MPMoviePlayerController. It's in the documentation. Can it be done with AVPlayer? Can I have two instances of AVPlayer, playing different movies, playing at the same time in my app with both movies visible to the user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感情洁癖 2024-11-24 09:03:06

使用 MPMoviePlayer 一次只能播放一部电影。但是,您可以使用 AVPlayer 同时播放多个视频(在 AVFoundation 中,较低级别的音频视频框架)。查看此

You can only play one movie at a time using MPMoviePlayer. However, you can play multiple videos simultaneously using AVPlayer (in AVFoundation, lower level audio-video framework).. check out this example:

昵称有卵用 2024-11-24 09:03:06

以下是一次播放 8 个,然后通过滚动一次再播放 8 个的方法:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];

// Enumerate array of visible cells' indexPaths to find a match
if ([self.collectionView.indexPathsForVisibleItems
     indexOfObjectPassingTest:^BOOL(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         return (obj.item == indexPath.item);
     }]) dispatch_async(dispatch_get_main_queue(), ^{
         [self drawLayerForPlayerForCell:cell atIndexPath:indexPath];
     });


    return cell;
}

- (void)drawPosterFrameForCell:(UICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    [self.imageManager requestImageForAsset:AppDelegate.sharedAppDelegate.assetsFetchResults[indexPath.item]
                                                          targetSize:AssetGridThumbnailSize
                                                         contentMode:PHImageContentModeAspectFill
                                                             options:nil
                                                       resultHandler:^(UIImage *result, NSDictionary *info) {
                                                           cell.contentView.layer.contents = (__bridge id)result.CGImage;
                                                       }];
}

- (void)drawLayerForPlayerForCell:(UICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    cell.contentView.layer.sublayers = nil;
    [self.imageManager requestPlayerItemForVideo:(PHAsset *)self.assetsFetchResults[indexPath.item] options:nil resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            if([[info objectForKey:PHImageResultIsInCloudKey] boolValue]) {
                [self drawPosterFrameForCell:cell atIndexPath:indexPath];
            } else {
                AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:[AVPlayer playerWithPlayerItem:playerItem]];
                [playerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
                [playerLayer setBorderColor:[UIColor whiteColor].CGColor];
                [playerLayer setBorderWidth:1.0f];
                [playerLayer setFrame:cell.contentView.layer.bounds];
                [cell.contentView.layer addSublayer:playerLayer];
                [playerLayer.player play];
            }
        });
    }];
}

请注意,drawPosterFrameForCell 方法将图像放置在无法播放视频的位置,因为它存储在 iCloud 上,并且不是设备。

Here's how you can play 8 at a time, and then play 8 more at a time by scrolling:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];

// Enumerate array of visible cells' indexPaths to find a match
if ([self.collectionView.indexPathsForVisibleItems
     indexOfObjectPassingTest:^BOOL(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         return (obj.item == indexPath.item);
     }]) dispatch_async(dispatch_get_main_queue(), ^{
         [self drawLayerForPlayerForCell:cell atIndexPath:indexPath];
     });


    return cell;
}

- (void)drawPosterFrameForCell:(UICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    [self.imageManager requestImageForAsset:AppDelegate.sharedAppDelegate.assetsFetchResults[indexPath.item]
                                                          targetSize:AssetGridThumbnailSize
                                                         contentMode:PHImageContentModeAspectFill
                                                             options:nil
                                                       resultHandler:^(UIImage *result, NSDictionary *info) {
                                                           cell.contentView.layer.contents = (__bridge id)result.CGImage;
                                                       }];
}

- (void)drawLayerForPlayerForCell:(UICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    cell.contentView.layer.sublayers = nil;
    [self.imageManager requestPlayerItemForVideo:(PHAsset *)self.assetsFetchResults[indexPath.item] options:nil resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            if([[info objectForKey:PHImageResultIsInCloudKey] boolValue]) {
                [self drawPosterFrameForCell:cell atIndexPath:indexPath];
            } else {
                AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:[AVPlayer playerWithPlayerItem:playerItem]];
                [playerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
                [playerLayer setBorderColor:[UIColor whiteColor].CGColor];
                [playerLayer setBorderWidth:1.0f];
                [playerLayer setFrame:cell.contentView.layer.bounds];
                [cell.contentView.layer addSublayer:playerLayer];
                [playerLayer.player play];
            }
        });
    }];
}

Note that the drawPosterFrameForCell method places an image where a video cannot be played because it is stored on iCloud, and not the device.

離殇 2024-11-24 09:03:06

MPMoviePlayerController 一次仅播放一个视频。

但是,您可以使用 AVPlayer 同时播放多个视频。

以下是同时播放多个视频的教程链接 - 同时播放视频

MPMoviePlayerController plays only one video at a time.

But, you can play multiple video simultaneously using AVPlayer.

Following are the tutorial link for playing multiple video simultaneously - Play video simultaneously

寻梦旅人 2024-11-24 09:03:06

如果您想在iOS应用程序中同时播放两个视频,请使用AVPlayer播放视频。

If you want to play two videos simultaneously in iOS app, use AVPlayer to play video.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文