fix(android): use UI thread to pause when lost audio focus (#3916)

This commit is contained in:
Olivier Bouillet 2024-06-20 11:50:56 +02:00 committed by GitHub
parent 264b57aa2e
commit 856b1dd58b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1172,12 +1172,16 @@ public class ReactExoplayerView extends FrameLayout implements
@Override @Override
public void onAudioFocusChange(int focusChange) { public void onAudioFocusChange(int focusChange) {
Activity activity = themedReactContext.getCurrentActivity();
switch (focusChange) { switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS: case AudioManager.AUDIOFOCUS_LOSS:
view.hasAudioFocus = false; view.hasAudioFocus = false;
view.eventEmitter.audioFocusChanged(false); view.eventEmitter.audioFocusChanged(false);
// FIXME this pause can cause issue if content doesn't have pause capability (can happen on live channel) // 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); view.audioManager.abandonAudioFocus(this);
break; break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
@ -1191,7 +1195,6 @@ public class ReactExoplayerView extends FrameLayout implements
break; break;
} }
Activity activity = themedReactContext.getCurrentActivity();
if (view.player != null && activity != null) { if (view.player != null && activity != null) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// Lower the volume // Lower the volume