feat: add plugins management (#3909)
This commit is contained in:
106
examples/react-native-video-plugin-sample/android/build.gradle
Normal file
106
examples/react-native-video-plugin-sample/android/build.gradle
Normal file
@@ -0,0 +1,106 @@
|
||||
buildscript {
|
||||
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
|
||||
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["VideoPluginSample_kotlinVersion"]
|
||||
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath "com.android.tools.build:gradle:7.2.1"
|
||||
// noinspection DifferentKotlinGradleVersion
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
def reactNativeArchitectures() {
|
||||
def value = rootProject.getProperties().get("reactNativeArchitectures")
|
||||
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
|
||||
}
|
||||
|
||||
def isNewArchitectureEnabled() {
|
||||
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
|
||||
}
|
||||
|
||||
apply plugin: "com.android.library"
|
||||
apply plugin: "kotlin-android"
|
||||
|
||||
if (isNewArchitectureEnabled()) {
|
||||
apply plugin: "com.facebook.react"
|
||||
}
|
||||
|
||||
def getExtOrDefault(name) {
|
||||
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["VideoPluginSample_" + name]
|
||||
}
|
||||
|
||||
def getExtOrIntegerDefault(name) {
|
||||
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["VideoPluginSample_" + name]).toInteger()
|
||||
}
|
||||
|
||||
def supportsNamespace() {
|
||||
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
|
||||
def major = parsed[0].toInteger()
|
||||
def minor = parsed[1].toInteger()
|
||||
|
||||
// Namespace support was added in 7.3.0
|
||||
return (major == 7 && minor >= 3) || major >= 8
|
||||
}
|
||||
|
||||
android {
|
||||
if (supportsNamespace()) {
|
||||
namespace "com.videopluginsample"
|
||||
|
||||
sourceSets {
|
||||
main {
|
||||
manifest.srcFile "src/main/AndroidManifestNew.xml"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
||||
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
||||
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
}
|
||||
}
|
||||
|
||||
lintOptions {
|
||||
disable "GradleCompatible"
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
google()
|
||||
}
|
||||
|
||||
def safeExtGet(prop) {
|
||||
return rootProject.ext.has(prop) ? rootProject.ext.get(prop) : project.properties["RNVideo_" + prop]
|
||||
}
|
||||
|
||||
def kotlin_version = getExtOrDefault("kotlinVersion")
|
||||
def media3_version = safeExtGet('media3Version')
|
||||
|
||||
dependencies {
|
||||
// For < 0.71, this will be from the local maven repo
|
||||
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
|
||||
//noinspection GradleDynamicVersion
|
||||
implementation "com.facebook.react:react-native:+"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation "androidx.media3:media3-exoplayer:$media3_version"
|
||||
implementation project(':react-native-video')
|
||||
}
|
||||
|
@@ -0,0 +1,5 @@
|
||||
VideoPluginSample_kotlinVersion=1.7.0
|
||||
VideoPluginSample_minSdkVersion=21
|
||||
VideoPluginSample_targetSdkVersion=31
|
||||
VideoPluginSample_compileSdkVersion=31
|
||||
VideoPluginSample_ndkversion=21.4.7075529
|
@@ -0,0 +1,3 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.videopluginsample">
|
||||
</manifest>
|
@@ -0,0 +1,2 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
</manifest>
|
@@ -0,0 +1,50 @@
|
||||
package com.videopluginsample
|
||||
|
||||
import androidx.media3.common.PlaybackException
|
||||
import androidx.media3.common.Player
|
||||
import androidx.media3.exoplayer.ExoPlayer
|
||||
import androidx.media3.exoplayer.util.EventLogger
|
||||
import com.brentvatne.common.toolbox.DebugLog
|
||||
import com.brentvatne.react.RNVPlugin
|
||||
import com.facebook.react.bridge.Promise
|
||||
import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
||||
import com.facebook.react.bridge.ReactMethod
|
||||
|
||||
class VideoPluginSampleModule(reactContext: ReactApplicationContext) :
|
||||
ReactContextBaseJavaModule(reactContext), RNVPlugin, Player.Listener {
|
||||
|
||||
private val debugEventLogger = EventLogger("RNVPluginSample")
|
||||
|
||||
override fun getName(): String {
|
||||
return NAME
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
fun setMetadata(promise: Promise) {
|
||||
promise.resolve(true)
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val NAME = "VideoPluginSample"
|
||||
const val TAG = "VideoPluginSampleModule"
|
||||
}
|
||||
|
||||
override fun onPlayerError(error: PlaybackException) {
|
||||
DebugLog.e(TAG, "onPlayerError: " + error.errorCodeName)
|
||||
}
|
||||
|
||||
|
||||
override fun onInstanceCreated(id: String, player: Any) {
|
||||
if (player is ExoPlayer) {
|
||||
player.addAnalyticsListener(debugEventLogger)
|
||||
player.addListener(this)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onInstanceRemoved(id: String, player: Any) {
|
||||
if (player is ExoPlayer) {
|
||||
player.removeAnalyticsListener(debugEventLogger)
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package com.videopluginsample
|
||||
|
||||
import com.brentvatne.react.ReactNativeVideoManager
|
||||
import com.facebook.react.ReactPackage
|
||||
import com.facebook.react.bridge.NativeModule
|
||||
import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.facebook.react.uimanager.ViewManager
|
||||
|
||||
class VideoPluginSamplePackage : ReactPackage {
|
||||
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
|
||||
val plugin = VideoPluginSampleModule(reactContext)
|
||||
ReactNativeVideoManager.getInstance().registerPlugin(plugin)
|
||||
return listOf(plugin)
|
||||
}
|
||||
|
||||
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
|
||||
return emptyList()
|
||||
}
|
||||
}
|
@@ -0,0 +1,2 @@
|
||||
#import <React/RCTBridgeModule.h>
|
||||
#import <React/RCTViewManager.h>
|
@@ -0,0 +1,14 @@
|
||||
#import <React/RCTBridgeModule.h>
|
||||
|
||||
@interface RCT_EXTERN_MODULE(VideoPluginSample, NSObject)
|
||||
|
||||
RCT_EXTERN_METHOD(setMetadata:
|
||||
withResolver:(RCTPromiseResolveBlock)resolve
|
||||
withRejecter:(RCTPromiseRejectBlock)reject)
|
||||
|
||||
+ (BOOL)requiresMainQueueSetup
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
@end
|
@@ -0,0 +1,67 @@
|
||||
import react_native_video
|
||||
import AVFoundation
|
||||
import AVKit
|
||||
|
||||
@objc(VideoPluginSample)
|
||||
class VideoPluginSample: NSObject, RNVPlugin {
|
||||
private var _playerRateChangeObserver: NSKeyValueObservation?
|
||||
private var _playerCurrentItemChangeObserver: NSKeyValueObservation?
|
||||
private var _playerItemStatusObserver: NSKeyValueObservation?
|
||||
|
||||
/**
|
||||
* create an init function to register the plugin
|
||||
*/
|
||||
override init() {
|
||||
super.init()
|
||||
ReactNativeVideoManager.shared.registerPlugin(plugin: self)
|
||||
}
|
||||
|
||||
|
||||
@objc(withResolver:withRejecter:)
|
||||
func setMetadata(resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
|
||||
resolve(true)
|
||||
}
|
||||
|
||||
/*
|
||||
* Handlers called on player creation and destructon
|
||||
*/
|
||||
func onInstanceCreated(id: String, player: Any) {
|
||||
if player is AVPlayer {
|
||||
let avPlayer = player as! AVPlayer
|
||||
NSLog("plug onInstanceCreated")
|
||||
_playerRateChangeObserver = avPlayer.observe(\.rate, options: [.old], changeHandler: handlePlaybackRateChange)
|
||||
_playerCurrentItemChangeObserver = avPlayer.observe(\.currentItem, options: [.old], changeHandler: handleCurrentItemChange)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func onInstanceRemoved(id: String, player: Any) {
|
||||
if player is AVPlayer {
|
||||
let avPlayer = player as! AVPlayer
|
||||
NSLog("plug onInstanceRemoved")
|
||||
_playerRateChangeObserver?.invalidate()
|
||||
_playerCurrentItemChangeObserver?.invalidate()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* custom functions to be able to track AVPlayer state change
|
||||
*/
|
||||
func handlePlaybackRateChange(player: AVPlayer, change: NSKeyValueObservedChange<Float>) {
|
||||
NSLog("plugin: handlePlaybackRateChange \(change.oldValue)")
|
||||
}
|
||||
|
||||
func handlePlayerItemStatusChange(playerItem: AVPlayerItem, change _: NSKeyValueObservedChange<AVPlayerItem.Status>) {
|
||||
NSLog("plugin: handlePlayerItemStatusChange \(playerItem.status)")
|
||||
}
|
||||
|
||||
func handleCurrentItemChange(player: AVPlayer, change: NSKeyValueObservedChange<AVPlayerItem?>) {
|
||||
NSLog("plugin: handleCurrentItemChange \(player.currentItem)")
|
||||
guard let playerItem = player.currentItem else {
|
||||
_playerItemStatusObserver?.invalidate()
|
||||
return
|
||||
}
|
||||
|
||||
_playerItemStatusObserver = playerItem.observe(\.status, options: [.new, .old], changeHandler: handlePlayerItemStatusChange)
|
||||
}
|
||||
}
|
9
examples/react-native-video-plugin-sample/package.json
Normal file
9
examples/react-native-video-plugin-sample/package.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "react-native-video-plugin-sample",
|
||||
"version": "0.0.0",
|
||||
"description": "sample subpackage for react native video plugin",
|
||||
"main": "src/index",
|
||||
"author": " <> ()",
|
||||
"license": "UNLICENSED",
|
||||
"homepage": "#readme"
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
require "json"
|
||||
|
||||
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
||||
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
|
||||
|
||||
Pod::Spec.new do |s|
|
||||
s.name = "react-native-video-plugin-sample"
|
||||
s.version = package["version"]
|
||||
s.summary = package["description"]
|
||||
s.homepage = package["homepage"]
|
||||
s.license = package["license"]
|
||||
s.authors = package["author"]
|
||||
|
||||
s.platforms = { :ios => min_ios_version_supported }
|
||||
s.source = { :git => ".git", :tag => "#{s.version}" }
|
||||
|
||||
s.source_files = "ios/**/*.{h,m,mm,swift}"
|
||||
s.dependency "react-native-video"
|
||||
|
||||
# Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
|
||||
# See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
|
||||
if respond_to?(:install_modules_dependencies, true)
|
||||
install_modules_dependencies(s)
|
||||
else
|
||||
s.dependency "React-Core"
|
||||
|
||||
# Don't install the dependencies when we run `pod install` in the old architecture.
|
||||
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
|
||||
s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
|
||||
s.pod_target_xcconfig = {
|
||||
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
|
||||
"OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1",
|
||||
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
|
||||
}
|
||||
s.dependency "React-Codegen"
|
||||
s.dependency "RCT-Folly"
|
||||
s.dependency "RCTRequired"
|
||||
s.dependency "RCTTypeSafety"
|
||||
s.dependency "ReactCommon/turbomodule/core"
|
||||
end
|
||||
end
|
||||
end
|
22
examples/react-native-video-plugin-sample/src/index.tsx
Normal file
22
examples/react-native-video-plugin-sample/src/index.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
import { NativeModules, Platform } from 'react-native';
|
||||
|
||||
const LINKING_ERROR =
|
||||
`The package 'react-native-video-plugin-sample' doesn't seem to be linked. Make sure: \n\n` +
|
||||
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
|
||||
'- You rebuilt the app after installing the package\n' +
|
||||
'- You are not using Expo Go\n';
|
||||
|
||||
const VideoPluginSample = NativeModules.VideoPluginSample
|
||||
? NativeModules.VideoPluginSample
|
||||
: new Proxy(
|
||||
{},
|
||||
{
|
||||
get() {
|
||||
throw new Error(LINKING_ERROR);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export function multiply(a: number, b: number): Promise<number> {
|
||||
return VideoPluginSample.multiply(a, b);
|
||||
}
|
Reference in New Issue
Block a user