【问题标题】:AVURLAsset gets initialized properly, but sometimes its associated AVPlayerLayer just renders blackAVURLAsset 被正确初始化,但有时其关联的 AVPlayerLayer 只是呈现黑色
【发布时间】:2012-02-19 03:10:27
【问题描述】:

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

这完美无缺,但只是有时。有趣的是,如果用户恰好选择了 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 次,它总是开始呈现黑屏。

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

【问题讨论】:

    标签: ios ipad rendering avplayer mediaplayback


    【解决方案1】:

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

    此 API 会导致渲染管道:

    +[AVPlayer playerWithPlayerItem:]
    

    【讨论】:

    • 您使用了什么解决方案?您是否一直只保留一个 AVPlayer 实例?
    • 我停止了所有播放器并释放了它们,然后切换到另一个页面,然后我在其中又创建了四个播放器。
    • 我的问题是我想在 UITableViewCell 中显示 AVPlayer。由于实例数量有限,我是否应该在每次想要播放时(滚动时)创建新的 AVPlayer?这会导致非常不稳定且看起来很糟糕的滚动。你知道如何解决这个问题吗?
    • 你有四个限制的引用吗?我已经成功地同时运行了多达 9 个 AVPlayer。
    • 这在最近的 iOS 版本中肯定有所改变。在 iOS 7 上,我一次最多可以在一个屏幕上显示八个。
    猜你喜欢
    • 1970-01-01
    • 2020-03-11
    • 2023-03-30
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多