From 5c0d9306b5903fb745b8ab6c9b01e236974bee55 Mon Sep 17 00:00:00 2001 From: Mario Guggenberger Date: Fri, 28 Oct 2016 10:17:14 +0200 Subject: [PATCH] Fix cache duration calculation --- .../android/mediaplayer/Decoders.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/MediaPlayer/src/main/java/net/protyposis/android/mediaplayer/Decoders.java b/MediaPlayer/src/main/java/net/protyposis/android/mediaplayer/Decoders.java index abaec43..7443b4b 100644 --- a/MediaPlayer/src/main/java/net/protyposis/android/mediaplayer/Decoders.java +++ b/MediaPlayer/src/main/java/net/protyposis/android/mediaplayer/Decoders.java @@ -163,13 +163,24 @@ public boolean isEOS() { } public long getCachedDuration() { - long minCachedDuration = -1; + // Init with the largest possible value... + long minCachedDuration = Long.MAX_VALUE; + + // ...then decrease to the lowest duration. + // We always return the lowest value, because if only one decoder has to refill its buffer, + // all others have to wait. If one decoder returns -1, this function returns -1 too (which + // makes sense because we cannot calculate a meaningful cache duration in this case). for (MediaCodecDecoder decoder : mDecoders) { long cachedDuration = decoder.getCachedDuration(); - if(cachedDuration != -1 && minCachedDuration > cachedDuration) { - minCachedDuration = cachedDuration; - } + minCachedDuration = Math.min(cachedDuration, minCachedDuration); } + + if(minCachedDuration == Long.MAX_VALUE) { + // There were no decoders that updated this value, which means we don't have information + // on a cached duration, so we return -1 to signal that the information is not available. + return -1; + } + return minCachedDuration; } }