From c9fd181199014f47f625184422bc12aa68adffed Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Mon, 31 Aug 2026 14:14:44 +0300 Subject: [PATCH] fix(android): cancel stale source initialization --- .../exoplayer/ReactExoplayerView.java | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index 9f5fd18673..0abd4b2f10 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -628,7 +628,7 @@ private void initializePlayer() { // This ensures all props have been settled, to avoid async racing conditions. Source runningSource = source; mainRunnable = () -> { - if (viewHasDropped && runningSource == source) { + if (viewHasDropped || runningSource != source) { return; } try { @@ -657,32 +657,36 @@ private void initializePlayer() { // DRM session manager creation must be done on a different thread to prevent crashes so we start a new thread ExecutorService es = Executors.newSingleThreadExecutor(); es.execute(() -> { - // DRM initialization must run on a different thread - if (viewHasDropped && runningSource == source) { - return; - } - if (activity == null) { - DebugLog.e(TAG, "Failed to initialize Player!, null activity"); - eventEmitter.onVideoError.invoke("Failed to initialize Player!", new Exception("Current Activity is null!"), "1001"); - return; - } - - // Initialize handler to run on the main thread - activity.runOnUiThread(() -> { - if (viewHasDropped && runningSource == source) { + try { + // DRM initialization must run on a different thread + if (viewHasDropped || runningSource != source) { return; } - try { - // Source initialization must run on the main thread - initializePlayerSource(runningSource); - } catch (Exception ex) { - self.playerNeedsSource = true; - DebugLog.e(TAG, "Failed to initialize Player! 1"); - DebugLog.e(TAG, ex.toString()); - ex.printStackTrace(); - eventEmitter.onVideoError.invoke(ex.toString(), ex, "1001"); + if (activity == null) { + DebugLog.e(TAG, "Failed to initialize Player!, null activity"); + eventEmitter.onVideoError.invoke("Failed to initialize Player!", new Exception("Current Activity is null!"), "1001"); + return; } - }); + + // Initialize handler to run on the main thread + activity.runOnUiThread(() -> { + if (viewHasDropped || runningSource != source) { + return; + } + try { + // Source initialization must run on the main thread + initializePlayerSource(runningSource); + } catch (Exception ex) { + self.playerNeedsSource = true; + DebugLog.e(TAG, "Failed to initialize Player! 1"); + DebugLog.e(TAG, ex.toString()); + ex.printStackTrace(); + eventEmitter.onVideoError.invoke(ex.toString(), ex, "1001"); + } + }); + } finally { + es.shutdown(); + } }); } else if (runningSource == source) { initializePlayerSource(runningSource);