From 91d27a6009a88e2d870f6a1cdbba04cd405bd85d Mon Sep 17 00:00:00 2001 From: Olivier Bouillet <62574056+freeboub@users.noreply.github.com> Date: Tue, 25 Jun 2024 08:55:32 +0200 Subject: [PATCH] feat: add plugins management (#3909) --- .../exoplayer/ReactExoplayerView.java | 6 + .../exoplayer/ReactExoplayerViewManager.java | 3 + .../java/com/brentvatne/react/RNVPlugin.kt | 22 +++ .../react/ReactNativeVideoManager.kt | 71 ++++++++++ docs/pages/other/_meta.json | 3 +- docs/pages/other/plugin.md | 125 ++++++++++++++++ examples/basic/android/app/build.gradle | 1 + .../java/com/videoplayer/MainApplication.kt | 2 + examples/basic/android/settings.gradle | 3 + examples/basic/ios/Podfile | 1 + examples/basic/ios/Podfile.lock | 32 ++++- .../ios/videoplayer.xcodeproj/project.pbxproj | 134 +++++++++--------- examples/basic/tsconfig.json | 1 + .../android/build.gradle | 106 ++++++++++++++ .../android/gradle.properties | 5 + .../android/src/main/AndroidManifest.xml | 3 + .../android/src/main/AndroidManifestNew.xml | 2 + .../VideoPluginSampleModule.kt | 50 +++++++ .../VideoPluginSamplePackage.kt | 19 +++ .../ios/VideoPluginSample-Bridging-Header.h | 2 + .../ios/VideoPluginSample.mm | 14 ++ .../ios/VideoPluginSample.swift | 67 +++++++++ .../package.json | 9 ++ .../react-native-video-plugin-sample.podspec | 42 ++++++ .../src/index.tsx | 22 +++ ios/Video/RCTVideo.swift | 7 + ios/Video/RNVPlugin.swift | 23 +++ ios/Video/ReactNativeVideoManager.swift | 53 +++++++ 28 files changed, 758 insertions(+), 70 deletions(-) create mode 100644 android/src/main/java/com/brentvatne/react/RNVPlugin.kt create mode 100644 android/src/main/java/com/brentvatne/react/ReactNativeVideoManager.kt create mode 100644 docs/pages/other/plugin.md create mode 100644 examples/react-native-video-plugin-sample/android/build.gradle create mode 100644 examples/react-native-video-plugin-sample/android/gradle.properties create mode 100644 examples/react-native-video-plugin-sample/android/src/main/AndroidManifest.xml create mode 100644 examples/react-native-video-plugin-sample/android/src/main/AndroidManifestNew.xml create mode 100644 examples/react-native-video-plugin-sample/android/src/main/java/com/videopluginsample/VideoPluginSampleModule.kt create mode 100644 examples/react-native-video-plugin-sample/android/src/main/java/com/videopluginsample/VideoPluginSamplePackage.kt create mode 100644 examples/react-native-video-plugin-sample/ios/VideoPluginSample-Bridging-Header.h create mode 100644 examples/react-native-video-plugin-sample/ios/VideoPluginSample.mm create mode 100644 examples/react-native-video-plugin-sample/ios/VideoPluginSample.swift create mode 100644 examples/react-native-video-plugin-sample/package.json create mode 100644 examples/react-native-video-plugin-sample/react-native-video-plugin-sample.podspec create mode 100644 examples/react-native-video-plugin-sample/src/index.tsx create mode 100644 ios/Video/RNVPlugin.swift create mode 100644 ios/Video/ReactNativeVideoManager.swift 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