From 4e7c64e7070dd319e5e77b937397742fd922cc4a Mon Sep 17 00:00:00 2001 From: Seyed Mostafa Hasani Date: Mon, 15 Jul 2024 13:00:53 +0330 Subject: [PATCH] refactor(android): migrate ReactExoplayerLoadErrorHandlingPolicy to Kotlin (#3995) * Rename .java to .kt * refactor(android): migrate ReactExoplayerLoadErrorHandlingPolicy to Kotlin --- ...ReactExoplayerLoadErrorHandlingPolicy.java | 36 ------------------- .../ReactExoplayerLoadErrorHandlingPolicy.kt | 27 ++++++++++++++ 2 files changed, 27 insertions(+), 36 deletions(-) delete mode 100644 android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerLoadErrorHandlingPolicy.java create mode 100644 android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerLoadErrorHandlingPolicy.kt diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerLoadErrorHandlingPolicy.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerLoadErrorHandlingPolicy.java deleted file mode 100644 index d1793b0b..00000000 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerLoadErrorHandlingPolicy.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.brentvatne.exoplayer; - -import androidx.media3.common.C; -import androidx.media3.datasource.HttpDataSource.HttpDataSourceException; -import androidx.media3.exoplayer.upstream.DefaultLoadErrorHandlingPolicy; - -public final class ReactExoplayerLoadErrorHandlingPolicy extends DefaultLoadErrorHandlingPolicy { - private final int minLoadRetryCount; - - public ReactExoplayerLoadErrorHandlingPolicy(int minLoadRetryCount) { - super(minLoadRetryCount); - this.minLoadRetryCount = minLoadRetryCount; - } - - @Override - public long getRetryDelayMsFor(LoadErrorInfo loadErrorInfo) { - String errorMessage = loadErrorInfo.exception.getMessage(); - - if ( - loadErrorInfo.exception instanceof HttpDataSourceException && - errorMessage != null && (errorMessage.equals("Unable to connect") || errorMessage.equals("Software caused connection abort")) - ) { - // Capture the error we get when there is no network connectivity and keep retrying it - return 1000; // Retry every second - } else if(loadErrorInfo.errorCount < this.minLoadRetryCount) { - return Math.min((loadErrorInfo.errorCount - 1) * 1000, 5000); // Default timeout handling - } else { - return C.TIME_UNSET; // Done retrying and will return the error immediately - } - } - - @Override - public int getMinimumLoadableRetryCount(int dataType) { - return Integer.MAX_VALUE; - } -} diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerLoadErrorHandlingPolicy.kt b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerLoadErrorHandlingPolicy.kt new file mode 100644 index 00000000..8b4cb77d --- /dev/null +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerLoadErrorHandlingPolicy.kt @@ -0,0 +1,27 @@ +package com.brentvatne.exoplayer + +import androidx.media3.common.C +import androidx.media3.datasource.HttpDataSource.HttpDataSourceException +import androidx.media3.exoplayer.upstream.DefaultLoadErrorHandlingPolicy +import androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy.LoadErrorInfo +import kotlin.math.min + +class ReactExoplayerLoadErrorHandlingPolicy(private val minLoadRetryCount: Int) : DefaultLoadErrorHandlingPolicy(minLoadRetryCount) { + override fun getRetryDelayMsFor(loadErrorInfo: LoadErrorInfo): Long { + val errorMessage: String? = loadErrorInfo.exception.message + + return if (loadErrorInfo.exception is HttpDataSourceException && + errorMessage != null && + (errorMessage == "Unable to connect" || errorMessage == "Software caused connection abort") + ) { + // Capture the error we get when there is no network connectivity and keep retrying it + 1000 // Retry every second + } else if (loadErrorInfo.errorCount < minLoadRetryCount) { + min(((loadErrorInfo.errorCount - 1) * 1000L), 5000L) // Default timeout handling + } else { + C.TIME_UNSET // Done retrying and will return the error immediately + } + } + + override fun getMinimumLoadableRetryCount(dataType: Int): Int = Int.MAX_VALUE +}