1. Brent Gulanowski
  2. NGMoviePlayer

Commits

Brent Gulanowski  committed 89d5d48

Use async asset property loading. KVO doesn't work.

  • Participants
  • Parent commits 21d9cd3
  • Branches default

Comments (0)

Files changed (1)

File Classes/NGMoviePlayerViewController.m

View file
 
 
 - (void)updateTimeRemaining {
+    if([self durationInSeconds] <= 0)
+        return;
 	self.timeRemainingLabel.text = [NSString stringWithFormat:@"-%@", timeStringForSeconds([self timeRemainingInSeconds])];	
 }
 
 	return [self durationInSeconds] - [self currentTimeInSeconds];
 }
 
-
-- (void)handleDurationDidChange {
-	self.movieDuration = self.player.currentItem.asset.duration;
-	[self updateControls];
-}
-
-
 - (void)handlePlayerStatusDidChange {
 	AVPlayerStatus playerStatus = self.player.status;
 	if (playerStatus == AVPlayerStatusReadyToPlay) {
 - (void)loadPlayer {
 	self.player = [[[AVPlayer alloc] initWithURL:self.contentURL] autorelease];
 	[self.player addObserver:self forKeyPath:@"status" options:0 context:nil];
-	[self.player addObserver:self forKeyPath:@"currentItem.asset.duration" options:0 context:nil];
 	[self.player addObserver:self forKeyPath:@"currentItem.error" options:0 context:nil];
 	self.player.actionAtItemEnd = AVPlayerActionAtItemEndPause;
+    [self.player.currentItem.asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"duration"]
+                                                 completionHandler:^{
+                                                     self.movieDuration = self.player.currentItem.asset.duration;
+                                                     [self updateControls];
+                                                 }];
 	[self addTimeObserver];
 }
 
 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
 	if ([keyPath isEqualToString:@"status"] && object == self.player)
 		[self handlePlayerStatusDidChange];
-	else if ([keyPath isEqualToString:@"currentItem.asset.duration"] && object == self.player)
-		[self handleDurationDidChange];
 	else if ([keyPath isEqualToString:@"currentItem.error"] && object == self.player)
 		[self handlePlayerError];
+    else
+        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
 }