Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

[Android] Handle audio focus changes while in a conference

Saúl Ibarra Corretgéab83a97fd53d
diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioModeModule.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioModeModule.java
index d626d0cf3d..64a6e18306 100644
--- a/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioModeModule.java
+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioModeModule.java
@@ -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) {