- (void)setPlayerUrl:(NSURL *)playerUrl
{
_playerUrl = playerUrl;
AVAsset *asset = [AVAsset assetWithURL:self.playerUrl];
NSArray *requestedKeys = [NSArray arrayWithObjects:kPlayableKey1, nil];
[asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler:
^{
dispatch_async( dispatch_get_main_queue(),
^{
for (NSString *thisKey in requestedKeys)
{
NSError *error = nil;
AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];
if (keyStatus == AVKeyValueStatusFailed)
{
return;
}
}
if (!asset.playable)
{
return;
}
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.playerLayer.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2, VIEWCONTAINER_HEIGHT);
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
[self.playerView.layer addSublayer:self.playerLayer];
[self.player play];
self.playerTimer = [NSTimer scheduledTimerWithTimeInterval:1 / TIMERUPDATE_RATE target:self selector:@selector(updatePreviewPlayerProgress) userInfo:nil repeats:true];
[[NSRunLoop currentRunLoop] addTimer:self.playerTimer forMode:NSRunLoopCommonModes];
[self.playerTimer fire];
[notificationCenter addObserver:self selector:@selector(videoPlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem];
});
}];
[self noticeAfterAllElementsLoaded];
}
-(void)videoPlayDidEnd:(NSNotification *)notification
{
[self.playerProgress setProgress:0 animated:true];
CMTime time = CMTimeMakeWithSeconds(0, NSEC_PER_SEC);
[self.player seekToTime:time];
[self.player play];
}