4

我目前正在使用MPMoviePlayerController在我的应用程序中播放视频。当我从一个视频切换到下一个视频时,过渡不是很顺畅。我得出的结论是,我需要做的是淡出第一个视频,同时淡入第二个视频。为此,我需要同时播放两个视频。我知道我不能这样做MPMoviePlayerController。它在文档中。可以用AVPlayer吗?我可以有两个实例AVPlayer,播放不同的电影,在我的应用程序中同时播放,用户可以看到两部电影吗?

4

4 回答 4

4

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

于 2012-01-24T17:39:25.420 回答
0

这是一次播放 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 上,而不是设备上。

于 2016-06-15T10:45:38.090 回答
-1

MPMoviePlayerController一次只播放一个视频。

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

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

于 2012-02-01T12:06:46.190 回答
-2

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

于 2012-06-22T06:53:53.960 回答