Created
August 30, 2012 18:35
-
-
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];
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////// | |
#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