Skip to content

Commit

Permalink
fix: ensure view drop stop playback startup (#3875)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofMoch committed Jun 7, 2024
1 parent 7133c96 commit ff1e24a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ public class ReactExoplayerView extends FrameLayout implements
private long lastBufferDuration = -1;
private long lastDuration = -1;

private boolean viewHasDropped = false;
private void updateProgress() {
if (player != null) {
if (playerControlView != null && isPlayingAd() && controls) {
Expand Down Expand Up @@ -375,6 +376,8 @@ protected void onDetachedFromWindow() {
public void cleanUpResources() {
stopPlayback();
themedReactContext.removeLifecycleEventListener(this);
releasePlayer();
viewHasDropped = true;
}

//BandwidthMeter.EventListener implementation
Expand Down Expand Up @@ -647,6 +650,9 @@ private void initializePlayer() {
Activity activity = themedReactContext.getCurrentActivity();
// This ensures all props have been settled, to avoid async racing conditions.
mainRunnable = () -> {
if (viewHasDropped) {
return;
}
try {
if (player == null) {
// Initialize core configuration and listeners
Expand All @@ -658,7 +664,9 @@ private void initializePlayer() {
ExecutorService es = Executors.newSingleThreadExecutor();
es.execute(() -> {
// DRM initialization must run on a different thread

if (viewHasDropped) {
return;
}
if (activity == null) {
DebugLog.e(TAG, "Failed to initialize Player!, null activity");
eventEmitter.error("Failed to initialize Player!", new Exception("Current Activity is null!"), "1001");
Expand All @@ -667,12 +675,15 @@ private void initializePlayer() {

// Initialize handler to run on the main thread
activity.runOnUiThread(() -> {
if (viewHasDropped) {
return;
}
try {
// Source initialization must run on the main thread
initializePlayerSource();
} catch (Exception ex) {
self.playerNeedsSource = true;
DebugLog.e(TAG, "Failed to initialize Player!");
DebugLog.e(TAG, "Failed to initialize Player! 1");
DebugLog.e(TAG, ex.toString());
ex.printStackTrace();
self.eventEmitter.error(ex.toString(), ex, "1001");
Expand All @@ -684,7 +695,7 @@ private void initializePlayer() {
}
} catch (Exception ex) {
self.playerNeedsSource = true;
DebugLog.e(TAG, "Failed to initialize Player!");
DebugLog.e(TAG, "Failed to initialize Player! 2");
DebugLog.e(TAG, ex.toString());
ex.printStackTrace();
eventEmitter.error(ex.toString(), ex, "1001");
Expand Down
4 changes: 2 additions & 2 deletions ios/Video/NowPlayingInfoCenterManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ class NowPlayingInfoCenterManager {
return
}

if let observer = observers[players.hashValue] {
if let observer = observers[player.hashValue] {
observer.invalidate()
}

observers.removeValue(forKey: players.hashValue)
observers.removeValue(forKey: player.hashValue)
players.remove(player)

if currentPlayer == player {
Expand Down
10 changes: 10 additions & 0 deletions ios/Video/RCTVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1241,17 +1241,27 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
// MARK: - Lifecycle

override func removeFromSuperview() {
self._player?.replaceCurrentItem(with: nil)
if let player = _player {
player.pause()
NowPlayingInfoCenterManager.shared.removePlayer(player: player)
}
_playerItem = nil
_source = nil
_chapters = nil
_drm = nil
_textTracks = nil
_selectedTextTrackCriteria = nil
_selectedAudioTrackCriteria = nil
_presentingViewController = nil

_player = nil
_resouceLoaderDelegate = nil
_playerObserver.clearPlayer()

#if USE_GOOGLE_IMA
_imaAdsManager.releaseAds()
_imaAdsManager = nil
#endif

self.removePlayerLayer()
Expand Down

0 comments on commit ff1e24a

Please sign in to comment.