Merge pull request #3042 from saghul/android-audiofocus

[Android] Handle audio focus changes while in a conference
This commit is contained in:
Zoltan Bettenbuk 2018-05-29 21:29:16 +02:00 committed by GitHub
commit 455660c891
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 3 deletions

View File

@ -57,7 +57,7 @@ import java.util.Set;
* Before a call has started and after it has ended the
* {@code AudioModeModule.DEFAULT} mode should be used.
*/
class AudioModeModule extends ReactContextBaseJavaModule {
class AudioModeModule extends ReactContextBaseJavaModule implements AudioManager.OnAudioFocusChangeListener {
/**
* Constants representing the audio mode.
* - DEFAULT: Used before and after every call. It represents the default
@ -97,6 +97,11 @@ class AudioModeModule extends ReactContextBaseJavaModule {
*/
static final String TAG = MODULE_NAME;
/**
* Indicator that we have lost audio focus.
*/
private boolean audioFocusLost = false;
/**
* {@link AudioManager} instance used to interact with the Android audio
* subsystem.
@ -345,6 +350,36 @@ class AudioModeModule extends ReactContextBaseJavaModule {
});
}
/**
* {@link AudioManager.OnAudioFocusChangeListener} interface method. Called
* when the audio focus of the system is updated.
*
* @param focusChange - The type of focus change.
*/
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN: {
Log.d(TAG, "Audio focus gained");
// Some other application potentially stole our audio focus
// temporarily. Restore our mode.
if (audioFocusLost) {
updateAudioRoute(mode);
}
audioFocusLost = false;
break;
}
case AudioManager.AUDIOFOCUS_LOSS:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: {
Log.d(TAG, "Audio focus lost");
audioFocusLost = true;
break;
}
}
}
/**
* Sets the user selected audio device as the active audio device.
*
@ -495,8 +530,9 @@ class AudioModeModule extends ReactContextBaseJavaModule {
Log.d(TAG, "Update audio route for mode: " + mode);
if (mode == DEFAULT) {
audioFocusLost = false;
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.abandonAudioFocus(null);
audioManager.abandonAudioFocus(this);
audioManager.setSpeakerphoneOn(false);
setBluetoothAudioRoute(false);
selectedDevice = null;
@ -509,7 +545,7 @@ class AudioModeModule extends ReactContextBaseJavaModule {
audioManager.setMicrophoneMute(false);
if (audioManager.requestAudioFocus(
null,
this,
AudioManager.STREAM_VOICE_CALL,
AudioManager.AUDIOFOCUS_GAIN)
== AudioManager.AUDIOFOCUS_REQUEST_FAILED) {