Skip to content

Commit

Permalink
Fix AudoFocus pausing video when attempting to play (#2311)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
armands-malejevs authored Jun 24, 2021
1 parent 1eb11ce commit fcc66df
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -567,7 +568,7 @@ private void releasePlayer() {
}

private boolean requestAudioFocus() {
if (disableFocus || srcUri == null) {
if (disableFocus || srcUri == null || this.hasAudioFocus) {
return true;
}
int result = audioManager.requestAudioFocus(this,
Expand All @@ -582,8 +583,8 @@ private void setPlayWhenReady(boolean playWhenReady) {
}

if (playWhenReady) {
boolean hasAudioFocus = requestAudioFocus();
if (hasAudioFocus) {
this.hasAudioFocus = requestAudioFocus();
if (this.hasAudioFocus) {
player.setPlayWhenReady(true);
}
} else {
Expand Down Expand Up @@ -678,6 +679,7 @@ private HttpDataSource.Factory buildHttpDataSourceFactory(boolean useBandwidthMe
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
this.hasAudioFocus = false;
eventEmitter.audioFocusChanged(false);
pausePlayback();
audioManager.abandonAudioFocus(this);
Expand All @@ -686,6 +688,7 @@ public void onAudioFocusChange(int focusChange) {
eventEmitter.audioFocusChanged(false);
break;
case AudioManager.AUDIOFOCUS_GAIN:
this.hasAudioFocus = true;
eventEmitter.audioFocusChanged(true);
break;
default:
Expand Down

0 comments on commit fcc66df

Please sign in to comment.