4

我有一个应用程序,用户可以在其中从本地视频文件中进行选择。当其中一个缩略图被推送时,会向用户呈现一个新视图,该视图具有我制作的自定义视频播放器,用于呈现视频。

这完美无缺,但只是有时。有趣的是,如果用户恰好选择了 5 次新视频(从而呈现新视图,初始化新的自定义视频播放器对象),则用于呈现播放器视觉效果的底层 AVPlayerLayer 呈现黑色,即使似乎基础资产仍然正确加载(播放器界面仍然保持视频的正确持续时间等等)。

当一个新的自定义媒体播放器对象被初始化时(当包含视图的媒体播放器的视图控制器被加载时发生),这是设置 AVPlayer 及其关联项的初始化方法的一部分:

    // 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!");
         }
     }];

现在,如果我准确地初始化这个自定义媒体播放器对象 5 次,它总是开始呈现黑屏。

有谁知道为什么会发生这种情况?

4

1 回答 1

5

这也咬到我了。AVFoundation 允许的并发视频播放器的数量是有限制的。这个数字是四个(对于 iOS 4.x,最近这个数字似乎有所增加。例如,在 iOS 7 上,我在一个屏幕上最多可以显示八个,没有问题)。这就是为什么它在第五个变黑的原因。你甚至不能假设你会得到四个,因为其他应用程序可能需要一个“渲染管道”。

此 API 会导致渲染管道:

+[AVPlayer playerWithPlayerItem:]
于 2012-04-14T04:43:29.523 回答