fix(android): ensure audio volume is changed in UI thread (#3292)

Co-authored-by: olivier <olivier.bouillet@ifeelsmart.com>
This commit is contained in:
Olivier Bouillet 2023-10-18 22:45:19 +02:00 committed by GitHub
parent c78077ff0a
commit 0bfbda66e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -281,7 +281,7 @@ public class ReactExoplayerView extends FrameLayout implements
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
themedReactContext.addLifecycleEventListener(this); themedReactContext.addLifecycleEventListener(this);
audioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver(themedReactContext); audioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver(themedReactContext);
audioFocusChangeListener = new OnAudioFocusChangedListener(this); audioFocusChangeListener = new OnAudioFocusChangedListener(this, themedReactContext);
} }
private boolean isPlayingAd() { private boolean isPlayingAd() {
@ -908,9 +908,11 @@ public class ReactExoplayerView extends FrameLayout implements
private static class OnAudioFocusChangedListener implements AudioManager.OnAudioFocusChangeListener { private static class OnAudioFocusChangedListener implements AudioManager.OnAudioFocusChangeListener {
private final ReactExoplayerView view; private final ReactExoplayerView view;
private final ThemedReactContext themedReactContext;
private OnAudioFocusChangedListener(ReactExoplayerView view) { private OnAudioFocusChangedListener(ReactExoplayerView view, ThemedReactContext themedReactContext) {
this.view = view; this.view = view;
this.themedReactContext = themedReactContext;
} }
@Override @Override
@ -919,6 +921,7 @@ public class ReactExoplayerView extends FrameLayout implements
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)
view.pausePlayback(); view.pausePlayback();
view.audioManager.abandonAudioFocus(this); view.audioManager.abandonAudioFocus(this);
break; break;
@ -933,16 +936,21 @@ public class ReactExoplayerView extends FrameLayout implements
break; break;
} }
if (view.player != null) { Activity activity = themedReactContext.getCurrentActivity();
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
if (!view.muted) { if (!view.muted) {
view.player.setVolume(view.audioVolume * 0.8f); activity.runOnUiThread(() -> {
view.player.setVolume(view.audioVolume * 0.8f);
});
} }
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// Raise it back to normal // Raise it back to normal
if (!view.muted) { if (!view.muted) {
view.player.setVolume(view.audioVolume * 1); activity.runOnUiThread(() -> {
view.player.setVolume(view.audioVolume * 1);
});
} }
} }
} }