Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```objective-c
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
- if ([keyPath isEqualToString:@"status"]) {
- if (object == _player && _player.status == AVPlayerStatusReadyToPlay) {
- }
- }
- }
- - (void)setupPlayer {
- AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@""]];
- _player = [[AVPlayer alloc] initWithPlayerItem:item];
- // [_player replaceCurrentItemWithPlayerItem:item];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playDidEnd) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
- [_player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
- [_player.currentItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
- __weak typeof(self) weakSelf = self;
- __weak AVPlayer *weakPlayer = _player;
- [_player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
- float currentTime = weakPlayer.currentItem.currentTime.value / weakPlayer.currentItem.currentTime.timescale;
- NSTimeInterval timeInterval = [weakSelf availableDuration];
- CMTime duration = weakPlayer.currentItem.duration;
- CGFloat totalDuration = CMTimeGetSeconds(duration);
- }];
- }
- - (NSTimeInterval)availableDuration {
- NSArray *loadedTimeRanges = [[_player currentItem] loadedTimeRanges];
- CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];
- float startSeconds = CMTimeGetSeconds(timeRange.start);
- float durationSeconds = CMTimeGetSeconds(timeRange.duration);
- NSTimeInterval result = startSeconds + durationSeconds;
- return result;
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement