From da4e5df5c808fdf7947e49fa0bdb84d6d3201dc2 Mon Sep 17 00:00:00 2001 From: Baris Sencan Date: Wed, 10 Jun 2015 14:29:38 -0700 Subject: [PATCH 1/2] Add playableDuration property to onProgress event --- RCTVideo.m | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/RCTVideo.m b/RCTVideo.m index c03debf3..b06d32f7 100644 --- a/RCTVideo.m +++ b/RCTVideo.m @@ -69,6 +69,7 @@ static NSString *const statusKeyPath = @"status"; (([_prevProgressUpdateTime timeIntervalSinceNow] * -1000.0) >= _progressUpdateInterval)) { [_eventDispatcher sendInputEventWithName:RNVideoEventProgress body:@{ @"currentTime": [NSNumber numberWithFloat:CMTimeGetSeconds(video.currentTime)], + @"playableDuration": [self calculatePlayableDuration], @"target": self.reactTag }]; @@ -76,6 +77,27 @@ static NSString *const statusKeyPath = @"status"; } } +/*! + * Calculates and returns the playable duration of the current player item using its loaded time ranges. + * + * \returns The playable duration of the current player item. + */ +- (NSNumber *)calculatePlayableDuration { + AVPlayerItem *video = _player.currentItem; + if (video.status == AVPlayerItemStatusReadyToPlay) { + __block CMTimeRange effectiveTimeRange; + [video.loadedTimeRanges enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + CMTimeRange timeRange = [obj CMTimeRangeValue]; + if (CMTimeRangeContainsTime(timeRange, video.currentTime)) { + effectiveTimeRange = timeRange; + *stop = YES; + } + }]; + return [NSNumber numberWithFloat:CMTimeGetSeconds(CMTimeRangeGetEnd(effectiveTimeRange))]; + } + return [NSNumber numberWithFloat:CMTimeGetSeconds(kCMTimeInvalid)]; +} + - (void)stopProgressTimer { [_progressUpdateTimer invalidate]; From d3c2867ab252433726d5e2115f302a79e71211bf Mon Sep 17 00:00:00 2001 From: Baris Sencan Date: Thu, 11 Jun 2015 18:11:55 -0700 Subject: [PATCH 2/2] Improve calculatePlayableDuration documentation --- RCTVideo.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RCTVideo.m b/RCTVideo.m index b06d32f7..68b7c4ab 100644 --- a/RCTVideo.m +++ b/RCTVideo.m @@ -80,7 +80,7 @@ static NSString *const statusKeyPath = @"status"; /*! * Calculates and returns the playable duration of the current player item using its loaded time ranges. * - * \returns The playable duration of the current player item. + * \returns The playable duration of the current player item in seconds. */ - (NSNumber *)calculatePlayableDuration { AVPlayerItem *video = _player.currentItem;