Cod*_*gle 4 rendering mediaplayback ipad ios avplayer
我有一个应用程序,用户可以在其中从本地视频文件中进行选择。当这些缩略图之一被推送时,用户会看到一个新视图,其中有一个我制作的用于显示视频的自定义视频播放器。
这可以完美地工作,但只是有时。有趣的是,如果用户选择一个新视频(从而呈现一个新视图,初始化一个新的自定义视频播放器对象)恰好 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!");
}
}];
Run Code Online (Sandbox Code Playgroud)
现在,如果我准确地初始化此自定义媒体播放器对象 5 次,它总是开始呈现黑屏。
有谁知道为什么会发生这种情况?
这也咬我了。AVFoundation 允许的并发视频播放器数量有限制。这个数字是 4(对于 iOS 4.x,最近这个数字似乎有所增加。例如,在 iOS 7 上,我在一个屏幕上最多可以显示 8 个,没有任何问题)。这就是为什么第五个它变黑的原因。您甚至不能假设您会得到四个,因为其他应用程序可能需要“渲染管道”。
此 API 会导致渲染管道:
+[AVPlayer playerWithPlayerItem:]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1497 次 |
| 最近记录: |