Browse Source

[Android] Handle audio focus changes while in a conference

j8
Saúl Ibarra Corretgé 7 years ago
parent
commit
ab83a97fd5
1 changed files with 39 additions and 3 deletions
  1. 39
    3
      android/sdk/src/main/java/org/jitsi/meet/sdk/AudioModeModule.java

+ 39
- 3
android/sdk/src/main/java/org/jitsi/meet/sdk/AudioModeModule.java View File

57
  * Before a call has started and after it has ended the
57
  * Before a call has started and after it has ended the
58
  * {@code AudioModeModule.DEFAULT} mode should be used.
58
  * {@code AudioModeModule.DEFAULT} mode should be used.
59
  */
59
  */
60
-class AudioModeModule extends ReactContextBaseJavaModule {
60
+class AudioModeModule extends ReactContextBaseJavaModule implements AudioManager.OnAudioFocusChangeListener {
61
     /**
61
     /**
62
      * Constants representing the audio mode.
62
      * Constants representing the audio mode.
63
      * - DEFAULT: Used before and after every call. It represents the default
63
      * - DEFAULT: Used before and after every call. It represents the default
97
      */
97
      */
98
     static final String TAG = MODULE_NAME;
98
     static final String TAG = MODULE_NAME;
99
 
99
 
100
+    /**
101
+     * Indicator that we have lost audio focus.
102
+     */
103
+    private boolean audioFocusLost = false;
104
+
100
     /**
105
     /**
101
      * {@link AudioManager} instance used to interact with the Android audio
106
      * {@link AudioManager} instance used to interact with the Android audio
102
      * subsystem.
107
      * subsystem.
345
         });
350
         });
346
     }
351
     }
347
 
352
 
353
+    /**
354
+     * {@link AudioManager.OnAudioFocusChangeListener} interface method. Called
355
+     * when the audio focus of the system is updated.
356
+     *
357
+     * @param focusChange - The type of focus change.
358
+     */
359
+    @Override
360
+    public void onAudioFocusChange(int focusChange) {
361
+        switch (focusChange) {
362
+        case AudioManager.AUDIOFOCUS_GAIN: {
363
+            Log.d(TAG, "Audio focus gained");
364
+            // Some other application potentially stole our audio focus
365
+            // temporarily. Restore our mode.
366
+            if (audioFocusLost) {
367
+                updateAudioRoute(mode);
368
+            }
369
+            audioFocusLost = false;
370
+            break;
371
+        }
372
+        case AudioManager.AUDIOFOCUS_LOSS:
373
+        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
374
+        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: {
375
+            Log.d(TAG, "Audio focus lost");
376
+            audioFocusLost = true;
377
+            break;
378
+        }
379
+
380
+        }
381
+    }
382
+
348
     /**
383
     /**
349
      * Sets the user selected audio device as the active audio device.
384
      * Sets the user selected audio device as the active audio device.
350
      *
385
      *
495
         Log.d(TAG, "Update audio route for mode: " + mode);
530
         Log.d(TAG, "Update audio route for mode: " + mode);
496
 
531
 
497
         if (mode == DEFAULT) {
532
         if (mode == DEFAULT) {
533
+            audioFocusLost = false;
498
             audioManager.setMode(AudioManager.MODE_NORMAL);
534
             audioManager.setMode(AudioManager.MODE_NORMAL);
499
-            audioManager.abandonAudioFocus(null);
535
+            audioManager.abandonAudioFocus(this);
500
             audioManager.setSpeakerphoneOn(false);
536
             audioManager.setSpeakerphoneOn(false);
501
             setBluetoothAudioRoute(false);
537
             setBluetoothAudioRoute(false);
502
             selectedDevice = null;
538
             selectedDevice = null;
509
         audioManager.setMicrophoneMute(false);
545
         audioManager.setMicrophoneMute(false);
510
 
546
 
511
         if (audioManager.requestAudioFocus(
547
         if (audioManager.requestAudioFocus(
512
-                    null,
548
+                    this,
513
                     AudioManager.STREAM_VOICE_CALL,
549
                     AudioManager.STREAM_VOICE_CALL,
514
                     AudioManager.AUDIOFOCUS_GAIN)
550
                     AudioManager.AUDIOFOCUS_GAIN)
515
                 == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
551
                 == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {

Loading…
Cancel
Save