Fix AudoFocus pausing video when attempting to play (#2311)

Fix AudioFocus bug that could cause the player to stop responding to play/pause in some instances.

Fixes issue #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.
This commit is contained in:
Armands Malejev 2021-06-24 08:00:38 +03:00 committed by GitHub
parent 1eb11ce7b7
commit fcc66df945
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 3 deletions

View File

@ -1,5 +1,7 @@
## Changelog ## 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 ### Version 5.1.0-alpha9
- Add ARM64 support for windows [#2137](https://github.com/react-native-community/react-native-video/pull/2137) - Add ARM64 support for windows [#2137](https://github.com/react-native-community/react-native-video/pull/2137)

View File

@ -122,6 +122,7 @@ class ReactExoplayerView extends FrameLayout implements
private boolean isPaused; private boolean isPaused;
private boolean isBuffering; private boolean isBuffering;
private boolean muted = false; private boolean muted = false;
private boolean hasAudioFocus = false;
private float rate = 1f; private float rate = 1f;
private float audioVolume = 1f; private float audioVolume = 1f;
private int minLoadRetryCount = 3; private int minLoadRetryCount = 3;
@ -567,7 +568,7 @@ class ReactExoplayerView extends FrameLayout implements
} }
private boolean requestAudioFocus() { private boolean requestAudioFocus() {
if (disableFocus || srcUri == null) { if (disableFocus || srcUri == null || this.hasAudioFocus) {
return true; return true;
} }
int result = audioManager.requestAudioFocus(this, int result = audioManager.requestAudioFocus(this,
@ -582,8 +583,8 @@ class ReactExoplayerView extends FrameLayout implements
} }
if (playWhenReady) { if (playWhenReady) {
boolean hasAudioFocus = requestAudioFocus(); this.hasAudioFocus = requestAudioFocus();
if (hasAudioFocus) { if (this.hasAudioFocus) {
player.setPlayWhenReady(true); player.setPlayWhenReady(true);
} }
} else { } else {
@ -678,6 +679,7 @@ class ReactExoplayerView extends FrameLayout implements
public void onAudioFocusChange(int focusChange) { public void onAudioFocusChange(int focusChange) {
switch (focusChange) { switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS: case AudioManager.AUDIOFOCUS_LOSS:
this.hasAudioFocus = false;
eventEmitter.audioFocusChanged(false); eventEmitter.audioFocusChanged(false);
pausePlayback(); pausePlayback();
audioManager.abandonAudioFocus(this); audioManager.abandonAudioFocus(this);
@ -686,6 +688,7 @@ class ReactExoplayerView extends FrameLayout implements
eventEmitter.audioFocusChanged(false); eventEmitter.audioFocusChanged(false);
break; break;
case AudioManager.AUDIOFOCUS_GAIN: case AudioManager.AUDIOFOCUS_GAIN:
this.hasAudioFocus = true;
eventEmitter.audioFocusChanged(true); eventEmitter.audioFocusChanged(true);
break; break;
default: default: