Fixed volume change when audio focus change

This commit is contained in:
Daniel Dudek 2018-11-14 12:33:28 +01:00
parent 2c391f5807
commit 5c7da30ddb

View File

@ -109,6 +109,7 @@ class ReactExoplayerView extends FrameLayout implements
private boolean isPaused; private boolean isPaused;
private boolean isBuffering; private boolean isBuffering;
private float rate = 1f; private float rate = 1f;
private float audioVolume = 1f;
private int minBufferMs = DefaultLoadControl.DEFAULT_MIN_BUFFER_MS; private int minBufferMs = DefaultLoadControl.DEFAULT_MIN_BUFFER_MS;
private int maxBufferMs = DefaultLoadControl.DEFAULT_MAX_BUFFER_MS; private int maxBufferMs = DefaultLoadControl.DEFAULT_MAX_BUFFER_MS;
@ -454,10 +455,10 @@ class ReactExoplayerView extends FrameLayout implements
if (player != null) { if (player != null) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// Lower the volume // Lower the volume
player.setVolume(0.8f); player.setVolume(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
player.setVolume(1); player.setVolume(audioVolume * 1);
} }
} }
} }
@ -875,15 +876,17 @@ class ReactExoplayerView extends FrameLayout implements
} }
public void setMutedModifier(boolean muted) { public void setMutedModifier(boolean muted) {
audioVolume = muted ? 0.f : 1.f;
if (player != null) { if (player != null) {
player.setVolume(muted ? 0 : 1); player.setVolume(audioVolume);
} }
} }
public void setVolumeModifier(float volume) { public void setVolumeModifier(float volume) {
audioVolume = volume;
if (player != null) { if (player != null) {
player.setVolume(volume); player.setVolume(audioVolume);
} }
} }