Skip to content

Commit 90c5789

Browse files
fix: memory leak (#439)
* fix: memory leak * fix: android remove bottomComponentView when release
1 parent 7a0e8e2 commit 90c5789

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

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

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ class ReactTVExoplayerView extends FrameLayout implements LifecycleEventListener
134134
Player.Listener,
135135
AnalyticsListener,
136136
BecomingNoisyListener,
137-
AudioManager.OnAudioFocusChangeListener,
138137
ExoDorisPlayerViewListener {
139138

140139
private static final String TAG = "ReactTvExoplayerView";
@@ -346,6 +345,33 @@ public void doFrame(long frameTimeNanos) {
346345
private final MediaSessionCompat mediaSession;
347346
private final MediaSessionConnector mediaSessionConnector;
348347

348+
private final AudioManager.OnAudioFocusChangeListener audioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
349+
@Override
350+
public void onAudioFocusChange(int focusChange) {
351+
switch (focusChange) {
352+
case AudioManager.AUDIOFOCUS_LOSS:
353+
eventEmitter.audioFocusChanged(false);
354+
break;
355+
case AudioManager.AUDIOFOCUS_GAIN:
356+
eventEmitter.audioFocusChanged(true);
357+
break;
358+
default:
359+
break;
360+
}
361+
362+
if (player != null) {
363+
ExoPlayer exoPlayer = player.getExoPlayer();
364+
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
365+
// Lower the volume
366+
exoPlayer.setVolume(0.8f);
367+
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
368+
// Raise it back to normal
369+
exoPlayer.setVolume(1);
370+
}
371+
}
372+
}
373+
};
374+
349375
public ReactTVExoplayerView(ThemedReactContext context) {
350376
super(context);
351377
this.themedReactContext = context;
@@ -814,8 +840,8 @@ public void onPlay() {
814840
public void onPause() {
815841
super.onPause();
816842
Log.d(TAG, "MediaSession onPause()");
817-
if (player == null || !isLive || player.getCurrentVideoType() == VideoType.LIVE_WITH_DVR){
818-
setPausedModifier(true);
843+
if (player == null || !isLive || player.getCurrentVideoType() == VideoType.LIVE_WITH_DVR) {
844+
setPausedModifier(true);
819845
}
820846
}
821847
});
@@ -864,23 +890,30 @@ private void releasePlayer() {
864890
trackSelector = null;
865891
clearResumePosition();
866892
}
867-
893+
if (dorisMessaging != null) {
894+
dorisMessaging.release();
895+
}
868896
jsProgressHandler.removeMessages(SHOW_JS_PROGRESS);
869897
nativeProgressHandler.removeMessages(SHOW_NATIVE_PROGRESS);
870898
themedReactContext.removeLifecycleEventListener(this);
871899
audioBecomingNoisyReceiver.removeListener();
872900
exoDorisPlayerView.setTag(R.id.bottomComponentTag, null);
901+
exoDorisPlayerView.setPlayer(null);
873902
View bottomOverlayView = exoDorisPlayerView.findViewWithTag(R.id.bottom_overlay_component);
874903
if (bottomOverlayView instanceof ReactRootView) {
875904
((ReactRootView) bottomOverlayView).unmountReactApplication();
876905
}
906+
ViewGroup bottomComponentContainer = exoDorisPlayerView.findViewById(R.id.bottomComponentContainer);
907+
if (bottomComponentContainer != null) {
908+
bottomComponentContainer.removeAllViews();
909+
}
877910
}
878911

879912
private boolean requestAudioFocus() {
880913
if (disableFocus) {
881914
return true;
882915
}
883-
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
916+
int result = audioManager.requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
884917
return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
885918
}
886919

@@ -966,7 +999,7 @@ private void stopPlayback() {
966999

9671000
private void onStopPlayback() {
9681001
setKeepScreenOn(false);
969-
audioManager.abandonAudioFocus(this);
1002+
audioManager.abandonAudioFocus(audioFocusChangeListener);
9701003
}
9711004

9721005
private void updateResumePosition() {
@@ -980,33 +1013,6 @@ private void clearResumePosition() {
9801013
resumePosition = ResumePositionHandler.RESUME_UNSET;
9811014
}
9821015

983-
// AudioManager.OnAudioFocusChangeListener implementation
984-
985-
@Override
986-
public void onAudioFocusChange(int focusChange) {
987-
switch (focusChange) {
988-
case AudioManager.AUDIOFOCUS_LOSS:
989-
eventEmitter.audioFocusChanged(false);
990-
break;
991-
case AudioManager.AUDIOFOCUS_GAIN:
992-
eventEmitter.audioFocusChanged(true);
993-
break;
994-
default:
995-
break;
996-
}
997-
998-
if (player != null) {
999-
ExoPlayer exoPlayer = player.getExoPlayer();
1000-
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
1001-
// Lower the volume
1002-
exoPlayer.setVolume(0.8f);
1003-
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
1004-
// Raise it back to normal
1005-
exoPlayer.setVolume(1);
1006-
}
1007-
}
1008-
}
1009-
10101016
// AudioBecomingNoisyListener implementation
10111017

10121018
@Override

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-video",
3-
"version": "7.10.14",
3+
"version": "7.10.15",
44
"dorisAndroidVersion": "3.14.8",
55
"messagingAndroidVersion": "1.1.0",
66
"description": "A <Video /> element for react-native",

0 commit comments

Comments
 (0)