Make sure audioOutput is respected on first render

This commit is contained in:
Craig Martin 2023-04-12 17:43:52 -04:00
parent daabb91475
commit e0095c3aff

View File

@ -694,6 +694,7 @@ class ReactExoplayerView extends FrameLayout implements
PlaybackParameters params = new PlaybackParameters(rate, 1f); PlaybackParameters params = new PlaybackParameters(rate, 1f);
player.setPlaybackParameters(params); player.setPlaybackParameters(params);
changeAudioOutput(this.audioOutput);
} }
private DrmSessionManager initializePlayerDrm(ReactExoplayerView self) { private DrmSessionManager initializePlayerDrm(ReactExoplayerView self) {
@ -1857,21 +1858,29 @@ class ReactExoplayerView extends FrameLayout implements
} }
} }
public void setAudioOutput(AudioOutput output) { private void changeAudioOutput(AudioOutput output) {
if (audioOutput != output && player != null) { if (player != null) {
this.audioOutput = output;
int usage = Util.getAudioUsageForStreamType(audioOutput.streamType); int usage = Util.getAudioUsageForStreamType(audioOutput.streamType);
int contentType = Util.getAudioContentTypeForStreamType(audioOutput.streamType); int contentType = Util.getAudioContentTypeForStreamType(audioOutput.streamType);
AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(usage).setContentType(contentType) AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(usage)
.setContentType(contentType)
.build(); .build();
player.setAudioAttributes(audioAttributes, false); player.setAudioAttributes(audioAttributes, false);
AudioManager audioManager = (AudioManager) themedReactContext.getSystemService(Context.AUDIO_SERVICE); AudioManager audioManager = (AudioManager) themedReactContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode( audioManager.setMode(
audioOutput == AudioOutput.SPEAKER ? AudioManager.MODE_NORMAL : AudioManager.MODE_IN_COMMUNICATION); audioOutput == AudioOutput.SPEAKER ? AudioManager.MODE_NORMAL
: AudioManager.MODE_IN_COMMUNICATION);
audioManager.setSpeakerphoneOn(audioOutput == AudioOutput.SPEAKER); audioManager.setSpeakerphoneOn(audioOutput == AudioOutput.SPEAKER);
} }
} }
public void setAudioOutput(AudioOutput output) {
if (audioOutput != output) {
this.audioOutput = output;
changeAudioOutput(output);
}
}
public void setVolumeModifier(float volume) { public void setVolumeModifier(float volume) {
audioVolume = volume; audioVolume = volume;
if (player != null) { if (player != null) {