开发者

Can AVPlayer instances be used to play two videos simultaneously?

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 simultane开发者_JAVA百科ously. 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?


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:


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜