Skip to content

Instantly share code, notes, and snippets.

@georgejecook
Created August 30, 2012 18:35
Show Gist options
  • Save georgejecook/3536957 to your computer and use it in GitHub Desktop.
Save georgejecook/3536957 to your computer and use it in GitHub Desktop.
something causes the code to crash at the alive4 in addPlayerLayer.. _playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
//////////////////////////////////////////////////////////////
#pragma mark video layer initialization
//////////////////////////////////////////////////////////////
- (void)initializePlayer {
[self cleanup:YES];
if (!self.player) {
self.player = [[AVPlayer alloc] init];
[self.layer setMasksToBounds:YES];
[self.player addObserver:self forKeyPath:@"status" options:0 context:nil];
}
//rebuild the project
[self pause];
AVPlayerItem *targetItem;
if (_previewItem) {
// DDLogVerbose(@"previewing item %@", _previewItem);
[self.player pause];
_itemDuration = _previewItem.asset.duration;
targetItem = [[AVPlayerItem alloc] initWithAsset:_previewItem.asset];
self.isSyncLayerHidden = YES;
} else {
[self.projectBuilder buildCompositionObjectsForTarget:self.buildTarget];
_itemDuration = self.project.duration;
targetItem = self.projectBuilder.playerItem;
[self updateSyncLayerPositionAndTransform];
}
[self.player replaceCurrentItemWithPlayerItem:targetItem];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
if (object == _player && [keyPath isEqualToString:@"status"]) {
DDLogVerbose(@"status changed ");
self.isSyncLayerHidden = !_previewItem && self.player.status != AVPlayerStatusReadyToPlay;
DDLogVerbose(@"alive1 ");
if (self.player.status == AVPlayerStatusReadyToPlay) {
DDLogVerbose(@"alive2 ");
[self addPlayerLayer];
}
DDLogVerbose(@"finished ");
}
}
- (void)addPlayerLayer {
DDLogVerbose(@"alive3 ");
DDLogVerbose(@"alive4 ");
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
DDLogVerbose(@"alive5 ");
_playerLayer.backgroundColor = [UIColor blackColor].CGColor;
DDLogVerbose(@"alive6 ");
_playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
_playerLayer.frame = self.bounds;
DDLogVerbose(@"alive7 ");
[self.layer addSublayer:self.playerLayer];
DDLogVerbose(@"alive8 ");
_isPlayerReady = YES;
}
- (void)cleanup:(BOOL)isFullCleanup {
_isPlayerReady = NO;
[self.player pause];
[self.player removeObserver:self forKeyPath:@"status"];
if (isFullCleanup) {
[self.playerLayer removeFromSuperlayer];
self.playerLayer = nil;
self.player = nil;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment