Skip to content

Commit 568971b

Browse files
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.
1 parent 5adedb1 commit 568971b

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Changelog
22

3+
- 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)
4+
35
### Version 5.1.0-alpha9
46

57
- Add ARM64 support for windows [#2137](https://github.com/react-native-community/react-native-video/pull/2137)

android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class ReactExoplayerView extends FrameLayout implements
122122
private boolean isPaused;
123123
private boolean isBuffering;
124124
private boolean muted = false;
125+
private boolean hasAudioFocus = false;
125126
private float rate = 1f;
126127
private float audioVolume = 1f;
127128
private int minLoadRetryCount = 3;
@@ -567,7 +568,7 @@ private void releasePlayer() {
567568
}
568569

569570
private boolean requestAudioFocus() {
570-
if (disableFocus || srcUri == null) {
571+
if (disableFocus || srcUri == null || this.hasAudioFocus) {
571572
return true;
572573
}
573574
int result = audioManager.requestAudioFocus(this,
@@ -582,8 +583,8 @@ private void setPlayWhenReady(boolean playWhenReady) {
582583
}
583584

584585
if (playWhenReady) {
585-
boolean hasAudioFocus = requestAudioFocus();
586-
if (hasAudioFocus) {
586+
this.hasAudioFocus = requestAudioFocus();
587+
if (this.hasAudioFocus) {
587588
player.setPlayWhenReady(true);
588589
}
589590
} else {
@@ -678,6 +679,7 @@ private HttpDataSource.Factory buildHttpDataSourceFactory(boolean useBandwidthMe
678679
public void onAudioFocusChange(int focusChange) {
679680
switch (focusChange) {
680681
case AudioManager.AUDIOFOCUS_LOSS:
682+
this.hasAudioFocus = false;
681683
eventEmitter.audioFocusChanged(false);
682684
pausePlayback();
683685
audioManager.abandonAudioFocus(this);
@@ -686,6 +688,7 @@ public void onAudioFocusChange(int focusChange) {
686688
eventEmitter.audioFocusChanged(false);
687689
break;
688690
case AudioManager.AUDIOFOCUS_GAIN:
691+
this.hasAudioFocus = true;
689692
eventEmitter.audioFocusChanged(true);
690693
break;
691694
default:

0 commit comments

Comments
 (0)