5

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?

iOSPawan
  • 2,884
  • 2
  • 25
  • 50
Ron
  • 51
  • 1
  • 3
  • Possible duplicate of [Multiple videos with AVPlayer](http://stackoverflow.com/questions/14646235/multiple-videos-with-avplayer) – Andres Canella May 10 '16 at 22:39

4 Answers4

4

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:

0

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.

mttrb
  • 8,297
  • 3
  • 35
  • 57
James Bush
  • 1,485
  • 14
  • 19
  • Here's my latest iteration of a perfectly smooth-scrolling collection view with real-time video previews (up to 16 at a time: https://youtu.be/7QlaO7WxjGg It even uses a cover flow custom layout and "reflection" view that mirrors the video preview perfectly. The source code is here: http://www.mediafire.com/download/ivecygnlhqxwynr/VideoWallCollectionView.zip – James Bush Aug 06 '16 at 02:56
  • Hm, when I run on my device, I just get a black screen. Very curious to see how you've pulled this off. – CIFilter Oct 11 '16 at 19:05
  • @LucasTizma 408-685-4049. You sure it's not a green screen? Anyway, I made a video of it for occasions like this one. Call me and I'll help you with the Xcode setup. – James Bush Oct 12 '16 at 00:31
-1

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

iOSPawan
  • 2,884
  • 2
  • 25
  • 50
-2

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

macpandit
  • 835
  • 1
  • 7
  • 11