Advertisement
Guest User

Untitled

a guest
May 30th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. ```objective-c
  2. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
  3. if ([keyPath isEqualToString:@"status"]) {
  4. if (object == _player && _player.status == AVPlayerStatusReadyToPlay) {
  5.  
  6. }
  7. }
  8. }
  9.  
  10. - (void)setupPlayer {
  11. AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@""]];
  12. _player = [[AVPlayer alloc] initWithPlayerItem:item];
  13.  
  14. // [_player replaceCurrentItemWithPlayerItem:item];
  15.  
  16. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playDidEnd) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
  17. [_player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
  18. [_player.currentItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
  19.  
  20. __weak typeof(self) weakSelf = self;
  21. __weak AVPlayer *weakPlayer = _player;
  22. [_player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
  23. float currentTime = weakPlayer.currentItem.currentTime.value / weakPlayer.currentItem.currentTime.timescale;
  24. NSTimeInterval timeInterval = [weakSelf availableDuration];
  25. CMTime duration = weakPlayer.currentItem.duration;
  26. CGFloat totalDuration = CMTimeGetSeconds(duration);
  27. }];
  28. }
  29.  
  30. - (NSTimeInterval)availableDuration {
  31. NSArray *loadedTimeRanges = [[_player currentItem] loadedTimeRanges];
  32. CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];
  33. float startSeconds = CMTimeGetSeconds(timeRange.start);
  34. float durationSeconds = CMTimeGetSeconds(timeRange.duration);
  35. NSTimeInterval result = startSeconds + durationSeconds;
  36.  
  37. return result;
  38. }
  39. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement