From 568971be7735ce5022c5c2691d5efd399aa4012b Mon Sep 17 00:00:00 2001 From: Armands Malejev Date: Mon, 15 Mar 2021 16:51:16 +0200 Subject: [PATCH] Fix AudoFocus pausing video when attempting to play (#1) Fix AudioFocus bug that could cause the player to stop responding to play/pause in some instances. Fixes issue react-native-video#1945 This was caused by the player requesting audio focus on each play (un-pause) and that resulted in a small window of Audio focus loss and then gain. The focus loss results in the player being paused while the player was supposed to play at the time. The solution is to keep track of Audio focus and not request new focus if we already have it. --- CHANGELOG.md | 2 ++ .../com/brentvatne/exoplayer/ReactExoplayerView.java | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53e2518c..731db2e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## Changelog +- Fix Android AudioFocus bug that could cause player to not respond to play/pause in some instances [#2311](https://github.com/react-native-video/react-native-video/pull/2311) + ### Version 5.1.0-alpha9 - Add ARM64 support for windows [#2137](https://github.com/react-native-community/react-native-video/pull/2137) 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 b41b2768..20ce983a 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -122,6 +122,7 @@ class ReactExoplayerView extends FrameLayout implements private boolean isPaused; private boolean isBuffering; private boolean muted = false; + private boolean hasAudioFocus = false; private float rate = 1f; private float audioVolume = 1f; private int minLoadRetryCount = 3; @@ -567,7 +568,7 @@ class ReactExoplayerView extends FrameLayout implements } private boolean requestAudioFocus() { - if (disableFocus || srcUri == null) { + if (disableFocus || srcUri == null || this.hasAudioFocus) { return true; } int result = audioManager.requestAudioFocus(this, @@ -582,8 +583,8 @@ class ReactExoplayerView extends FrameLayout implements } if (playWhenReady) { - boolean hasAudioFocus = requestAudioFocus(); - if (hasAudioFocus) { + this.hasAudioFocus = requestAudioFocus(); + if (this.hasAudioFocus) { player.setPlayWhenReady(true); } } else { @@ -678,6 +679,7 @@ class ReactExoplayerView extends FrameLayout implements public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS: + this.hasAudioFocus = false; eventEmitter.audioFocusChanged(false); pausePlayback(); audioManager.abandonAudioFocus(this); @@ -686,6 +688,7 @@ class ReactExoplayerView extends FrameLayout implements eventEmitter.audioFocusChanged(false); break; case AudioManager.AUDIOFOCUS_GAIN: + this.hasAudioFocus = true; eventEmitter.audioFocusChanged(true); break; default: