Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public ReactExoplayerView(ThemedReactContext context, ReactExoplayerConfig confi
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
themedReactContext.addLifecycleEventListener(this);
audioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver(themedReactContext);
audioFocusChangeListener = new OnAudioFocusChangedListener(this);
audioFocusChangeListener = new OnAudioFocusChangedListener(this, themedReactContext);
}

private boolean isPlayingAd() {
Expand Down Expand Up @@ -910,9 +910,11 @@ private void releasePlayer() {

private static class OnAudioFocusChangedListener implements AudioManager.OnAudioFocusChangeListener {
private final ReactExoplayerView view;
private final ThemedReactContext themedReactContext;

private OnAudioFocusChangedListener(ReactExoplayerView view) {
private OnAudioFocusChangedListener(ReactExoplayerView view, ThemedReactContext themedReactContext) {
this.view = view;
this.themedReactContext = themedReactContext;
}

@Override
Expand All @@ -921,6 +923,7 @@ public void onAudioFocusChange(int 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();
view.audioManager.abandonAudioFocus(this);
break;
Expand All @@ -935,16 +938,21 @@ public void onAudioFocusChange(int focusChange) {
break;
}

if (view.player != null) {
Activity activity = themedReactContext.getCurrentActivity();
if (view.player != null && activity != null) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// Lower the volume
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) {
// Raise it back to normal
if (!view.muted) {
view.player.setVolume(view.audioVolume * 1);
activity.runOnUiThread(() -> {
view.player.setVolume(view.audioVolume * 1);
});
}
}
}
Expand Down