Skip to content

Instantly share code, notes, and snippets.

@gatamar
Created May 22, 2017 18:57
Show Gist options
  • Save gatamar/508ef23932b2408f55b2aad2b6d0e279 to your computer and use it in GitHub Desktop.
Save gatamar/508ef23932b2408f55b2aad2b6d0e279 to your computer and use it in GitHub Desktop.
// subscribe for KVO somewhere
[_player addObserver:self
forKeyPath:@"currentItem.loadedTimeRanges"
options:NSKeyValueObservingOptionNew
context:nil];
...
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)contex
{
...
if (object == _player && [keyPath isEqualToString:@"currentItem.loadedTimeRanges"])
{
NSArray *timeRanges = (NSArray*)[change objectForKey:NSKeyValueChangeNewKey];
if (timeRanges && [timeRanges count])
{
CMTimeRange timerange;
NSValue* val = [timeRanges objectAtIndex:0];
[val getValue:&timerange];
float currentBufferDuration = CMTimeGetSeconds(CMTimeAdd(timerange.start, timerange.duration));
CMTime duration = _player.currentItem.asset.duration;
float seconds = CMTimeGetSeconds(duration);
float hours=seconds/3600;
NSLog(@"Movie takes %.2f hours", hours);
if (currentBufferDuration == seconds)
{
// Ready to play.
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment