Question

I have an application in which the user can select from local video files. When one of those thumbnails gets pushed, the user is presented a new view which have a custom video player I've made that presents the video.

This works flawlessly, but only sometimes. The funny thing is that if the user selects a new video (thus getting presented a new view, initializing a new custom video player object) exactly 5 times, the underlying AVPlayerLayer that is used to present the visuals from the player renders black, even though it seems like the underlying asset still loads correctly (the player interface still holds the correct duration for the video and so forth).

When a new custom media player object gets initialized (which happens when the view controller for the media players containing view gets loaded), this is the part of the initializer method which sets up the AVPlayer and its associated item:

    // Start to load the specified asset
    mediaAsset = [[AVURLAsset alloc] initWithURL:contentURL options:nil];

    if (mediaAsset == nil)
        NSLog(@"The media asset is zero!!!");

    // Now we need to asynchronously load in the tracks of the specified asset (like audio and video tracks). We load them asynchronously to avoid having the entire app UI freeze while loading occours
    NSString* keyValueToLoad = @"tracks";

    // When loading the tracks asynchronously we also specify a completionHandler, which is the block of code that should be executed once the loading is either or for some reason failed
    [mediaAsset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:keyValueToLoad
                                                 ] completionHandler:^
     {
         // When this block gets executed we check for potential errors or see if the asset loaded successfully
         NSError* error = nil;

         AVKeyValueStatus trackStatus = [mediaAsset statusOfValueForKey:keyValueToLoad error:&error];

         if (error != nil)
         {
             NSLog(@"Error: %@", error.description);
         }

         //switch (trackStatus) {
             //case AVKeyValueStatusLoaded:
         if (trackStatus == AVKeyValueStatusLoaded)
         {
                 NSLog(@"Did load properly!");
                 mediaItem = [AVPlayerItem playerItemWithAsset:mediaAsset];

                if (mediaItem.error == nil)
                    NSLog(@"Everything went fine!");

                if (mediaItem == nil)
                    NSLog(@"THE MEDIA ITEM WAS NIL");

                 [mediaItem addObserver:self forKeyPath:@"status" options:0 context:&itemStatusContext];

                     mediaContentPlayer = [[AVPlayer alloc] initWithPlayerItem:mediaItem];

                         [mediaContentView setPlayer:mediaContentPlayer];

             //mediaContentView = [AVPlayerLayer playerLayerWithPlayer:mediaContentPlayer];

                [activeModeViewBlocked configurePlaybackSliderWithDuration:mediaItem.duration];

                 originalDuration = mediaItem.duration.value / mediaItem.duration.timescale;

                 // We will subscribe to a timeObserver on the player to check for the current playback time of the movie within a specified interval. Doing so will allow us to frequently update the user interface with correct information
                 playbackTimeObserver = [mediaContentPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 50) queue:dispatch_get_main_queue() usingBlock:^(CMTime time)
                                         {
                                             NSLog(@"TIME UPDATED!");
                                             [activeModeViewBlocked updatePlaybackSlider:time];
                                         }];

                 [self syncUI];
         }

         if (trackStatus == AVKeyValueStatusFailed)
         {
             NSLog(@"Something failed!");
         }

         if (trackStatus == AVKeyValueStatusCancelled)
         {
             NSLog(@"Something was cancelled!");
         }
     }];

Now if I initialize this custom media player object 5 times exactly, it always starts to render black screens.

Does anyone have any idea of why this could be happening?

Was it helpful?

Solution

This bit me too. There is a limit on the number of concurrent video players that AVFoundation will allow. That number is four(for iOS 4.x, more recently the number seems to have increased. For example, on iOS 7 I've had up to eight on one screen with no issue). That is why it is going black on the fifth one. You can't even assume you'll get four, as other apps may need a 'render pipeline'.

This API causes a render pipeline:

+[AVPlayer playerWithPlayerItem:]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top