From 82c191d97fdf38580b993aba030e1de354aee886 Mon Sep 17 00:00:00 2001 From: Michael Tintiuc Date: Mon, 17 Feb 2020 18:53:56 +0200 Subject: [PATCH] Improve Android Audio Focus (#1897) Implement audio focus as per android docs: https://developer.android.com/guide/topics/media-apps/audio-focus https://medium.com/androiddevelopers/audio-focus-3-cdc09da9c122 AUDIOFOCUS_LOSS should abandon focus and not try resuming audio, this is done with AUDIOFOCUS_LOSS_TRANSIENT This fixes at least: - Audio not being paused after focus being taken by some voip applications - Content resuming and pausing instantly sporadically (some race condition perhaps) when activity was resumed from background. --- .../java/com/brentvatne/exoplayer/ReactExoplayerView.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index 52f7567f16..a03b0fbba5 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -624,6 +624,11 @@ private DataSource.Factory buildDataSourceFactory(boolean useBandwidthMeter) { public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: + eventEmitter.audioFocusChanged(false); + pausePlayback(); + audioManager.abandonAudioFocus(this); + break; + case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: eventEmitter.audioFocusChanged(false); break; case AudioManager.AUDIOFOCUS_GAIN: