diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index e0a14a71d4..ef6c88784f 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -123,6 +123,7 @@ import com.brentvatne.common.toolbox.DebugLog; import com.brentvatne.react.BuildConfig; import com.brentvatne.react.R; +import com.brentvatne.react.ReactNativeVideoManager; import com.brentvatne.receiver.AudioBecomingNoisyReceiver; import com.brentvatne.receiver.BecomingNoisyListener; import com.facebook.react.bridge.LifecycleEventListener; @@ -258,6 +259,9 @@ public class ReactExoplayerView extends FrameLayout implements private long lastDuration = -1; private boolean viewHasDropped = false; + + private String instanceId = String.valueOf(UUID.randomUUID()); + private void updateProgress() { if (player != null) { if (playerControlView != null && isPlayingAd() && controls) { @@ -756,6 +760,7 @@ private void initializePlayerCore(ReactExoplayerView self) { .setLoadControl(loadControl) .setMediaSourceFactory(mediaSourceFactory) .build(); + ReactNativeVideoManager.Companion.getInstance().onInstanceCreated(instanceId, player); refreshDebugState(); player.addListener(self); player.setVolume(muted ? 0.f : audioVolume * 1); @@ -1150,6 +1155,7 @@ private void releasePlayer() { player.removeListener(this); trackSelector = null; + ReactNativeVideoManager.Companion.getInstance().onInstanceRemoved(instanceId, player); player = null; } diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java index c43349b9c6..f23ea54817 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java @@ -20,6 +20,7 @@ import com.brentvatne.common.react.VideoEventEmitter; import com.brentvatne.common.toolbox.DebugLog; import com.brentvatne.common.toolbox.ReactBridgeUtils; +import com.brentvatne.react.ReactNativeVideoManager; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.common.MapBuilder; @@ -97,12 +98,14 @@ public String getName() { @NonNull @Override protected ReactExoplayerView createViewInstance(@NonNull ThemedReactContext themedReactContext) { + ReactNativeVideoManager.Companion.getInstance().registerView(this); return new ReactExoplayerView(themedReactContext, config); } @Override public void onDropViewInstance(ReactExoplayerView view) { view.cleanUpResources(); + ReactNativeVideoManager.Companion.getInstance().unregisterView(this); } @Override diff --git a/android/src/main/java/com/brentvatne/react/RNVPlugin.kt b/android/src/main/java/com/brentvatne/react/RNVPlugin.kt new file mode 100644 index 0000000000..2cdf676608 --- /dev/null +++ b/android/src/main/java/com/brentvatne/react/RNVPlugin.kt @@ -0,0 +1,22 @@ +package com.brentvatne.react + +/** + * Plugin interface definition + */ +interface RNVPlugin { + /** + * Function called when a new player is created + * @param id: a random string identifying the player + * @param player: the instantiated player reference + */ + fun onInstanceCreated(id: String, player: Any) + + /** + * Function called when a player should be destroyed + * when this callback is called, the plugin shall free all + * resources and release all reference to Player object + * @param id: a random string identifying the player + * @param player: the player to release + */ + fun onInstanceRemoved(id: String, player: Any) +} diff --git a/android/src/main/java/com/brentvatne/react/ReactNativeVideoManager.kt b/android/src/main/java/com/brentvatne/react/ReactNativeVideoManager.kt new file mode 100644 index 0000000000..d279264302 --- /dev/null +++ b/android/src/main/java/com/brentvatne/react/ReactNativeVideoManager.kt @@ -0,0 +1,71 @@ +package com.brentvatne.react + +import com.brentvatne.common.toolbox.DebugLog +import com.brentvatne.exoplayer.ReactExoplayerViewManager + +/** + * ReactNativeVideoManager is a singleton class which allows to manipulate / the global state of the app + * It handles the list of