From f031e36403b7b79c06016b86ff95d6d6285775a2 Mon Sep 17 00:00:00 2001 From: Tomas Maly Date: Sun, 26 Nov 2017 01:12:10 -0800 Subject: [PATCH] Update AudioPlayer.m Fixing player status to not show ready when not quite (loading remotely) - causes inaccurate currentTime --- .../ReactNativeAudioToolkit/AudioPlayer.m | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ios/ReactNativeAudioToolkit/ReactNativeAudioToolkit/AudioPlayer.m b/ios/ReactNativeAudioToolkit/ReactNativeAudioToolkit/AudioPlayer.m index a5b8732f..3e77e9a6 100644 --- a/ios/ReactNativeAudioToolkit/ReactNativeAudioToolkit/AudioPlayer.m +++ b/ios/ReactNativeAudioToolkit/ReactNativeAudioToolkit/AudioPlayer.m @@ -192,9 +192,32 @@ - (NSURL *)findUrlForPath:(NSString *)path { while (player.status == AVPlayerStatusUnknown) { [NSThread sleepForTimeInterval:0.01f]; } + + //make sure loadedTimeRanges is not null + while (player.currentItem.loadedTimeRanges.firstObject == nil){ + [NSThread sleepForTimeInterval:0.01f]; + } + + float version = [[[UIDevice currentDevice] systemVersion] floatValue]; + + //wait until 10 seconds are buffered then play + if (version >= 10.0) { + player.currentItem.preferredForwardBufferDuration = 500; + } + Float64 durationSeconds = 0; + while (durationSeconds < 10){ + NSValue *val = player.currentItem.loadedTimeRanges.firstObject; + CMTimeRange timeRange; + [val getValue:&timeRange]; + durationSeconds = CMTimeGetSeconds(timeRange.duration); + [NSThread sleepForTimeInterval:0.01f]; + } // Callback when ready / failed if (player.status == AVPlayerStatusReadyToPlay) { + if (version >= 10.0) { + player.automaticallyWaitsToMinimizeStalling = false; + } callback(@[[NSNull null]]); } else { NSDictionary* dict = [Helpers errObjWithCode:@"preparefail"