我遇到的问题是,当我使用 iOS9 sdk 编译我的应用程序时,当我的应用程序尝试CVPixelBufferRef
从一个函数中获取一个时,当加载视频并创建所有实例时AVPlayerItemVideoOutput
,- copyPixelBufferForItemTime:itemTimeForDisplay:
我不时得到一个空值。
使用 iOS 8,我的应用程序运行良好,但使用 iOS9 时出现问题,即使应用商店中可供下载且使用 iOS 8 SDK 编译的应用程序版本在安装时也会出现同样的问题IOS9。
当问题发生并且我得到一个 nullgetCVPixelBufferRef
时,如果我按下主页按钮并且应用程序在我再次打开应用程序并变为活动状态时进入后台,AVPlayerItemVideoOutput
那么给我 null 的实例CVPixelBufferRef
开始正常工作并且问题得到解决。
这是一个 youtube 视频,我在其中复制了该问题:
https://www.youtube.com/watch?v=997zG08_DMM&feature=youtu.be
以下是创建所有项目实例的示例代码:
NSURL *url ;
url = [[NSURL alloc] initFileURLWithPath:[_mainVideo objectForKey:@"file"]];
NSDictionary *pixBuffAttributes = @{(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)};
_videoOutput = [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:pixBuffAttributes];
_myVideoOutputQueue = dispatch_queue_create("myVideoOutputQueue", DISPATCH_QUEUE_SERIAL);
[_videoOutput setDelegate:self queue:_myVideoOutputQueue];
_player = [[AVPlayer alloc] init];
// Do not take mute button into account
NSError *error = nil;
BOOL success = [[AVAudioSession sharedInstance]
setCategory:AVAudioSessionCategoryPlayback
error:&error];
if (!success) {
// NSLog(@"Could not use AVAudioSessionCategoryPlayback", nil);
}
asset = [AVURLAsset URLAssetWithURL:url options:nil];
if(![[NSFileManager defaultManager] fileExistsAtPath:[[asset URL] path]]) {
// NSLog(@"file does not exist");
}
NSArray *requestedKeys = [NSArray arrayWithObjects:kTracksKey, kPlayableKey, nil];
[asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler:^{
dispatch_async( dispatch_get_main_queue(),
^{
/* Make sure that the value of each key has loaded successfully. */
for (NSString *thisKey in requestedKeys)
{
NSError *error = nil;
AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];
if (keyStatus == AVKeyValueStatusFailed)
{
[self assetFailedToPrepareForPlayback:error];
return;
}
}
NSError* error = nil;
AVKeyValueStatus status = [asset statusOfValueForKey:kTracksKey error:&error];
if (status == AVKeyValueStatusLoaded)
{
//_playerItem = [AVPlayerItem playerItemWithAsset:asset];
[_playerItem addOutput:_videoOutput];
[_player replaceCurrentItemWithPlayerItem:_playerItem];
[_videoOutput requestNotificationOfMediaDataChangeWithAdvanceInterval:ONE_FRAME_DURATION];
/* When the player item has played to its end time we'll toggle
the movie controller Pause button to be the Play button */
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:_playerItem];
seekToZeroBeforePlay = NO;
[_playerItem addObserver:self
forKeyPath:kStatusKey
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:AVPlayerDemoPlaybackViewControllerStatusObservationContext];
[_player addObserver:self
forKeyPath:kCurrentItemKey
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:AVPlayerDemoPlaybackViewControllerCurrentItemObservationContext];
[_player addObserver:self
forKeyPath:kRateKey
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:AVPlayerDemoPlaybackViewControllerRateObservationContext];
[self initScrubberTimer];
[self syncScrubber];
}
else
{
// NSLog(@"%@ Failed to load the tracks.", self);
}
});
}];
这是给我空像素缓冲区的示例代码
CVPixelBufferRef pixelBuffer =
[_videoOutput
copyPixelBufferForItemTime:[_playerItem currentTime]
itemTimeForDisplay:nil];
NSLog(@"the pixel buffer is %@", pixelBuffer);
NSLog (@"the _videoOutput is %@", _videoOutput.description);
CMTime dataTime = [_playerItem currentTime];
//NSLog(@"the current time is %f", dataTime);
return pixelBuffer;