From 856b1dd58ba9bb1832397ee0fc86f778f737798f Mon Sep 17 00:00:00 2001 From: Olivier Bouillet <62574056+freeboub@users.noreply.github.com> Date: Thu, 20 Jun 2024 11:50:56 +0200 Subject: [PATCH] fix(android): use UI thread to pause when lost audio focus (#3916) --- .../java/com/brentvatne/exoplayer/ReactExoplayerView.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index 0d6934f1..27a3478e 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -1172,12 +1172,16 @@ public class ReactExoplayerView extends FrameLayout implements @Override public void onAudioFocusChange(int focusChange) { + Activity activity = themedReactContext.getCurrentActivity(); + switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: view.hasAudioFocus = false; view.eventEmitter.audioFocusChanged(false); // FIXME this pause can cause issue if content doesn't have pause capability (can happen on live channel) - view.pausePlayback(); + if (activity != null) { + activity.runOnUiThread(view::pausePlayback); + } view.audioManager.abandonAudioFocus(this); break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: @@ -1191,7 +1195,6 @@ public class ReactExoplayerView extends FrameLayout implements break; } - Activity activity = themedReactContext.getCurrentActivity(); if (view.player != null && activity != null) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { // Lower the volume