@@ -34,29 +34,67 @@ npx create-react-native-library@latest react-native-video-custom-analytics
34
34
Both Android and iOS implementations expose an ` RNVPlugin ` interface.
35
35
Your ` react-native-video-custom-analytics ` package should implement this interface and register itself as a plugin for ` react-native-video ` .
36
36
37
+ ## Plugin Types
38
+
39
+ There are two types of plugins you can implement:
40
+
41
+ 1 . ** Base Plugin (` RNVPlugin ` )** : For general-purpose plugins that don't need specific player implementation details.
42
+ 2 . ** Player-Specific Plugins** :
43
+ - ` RNVAVPlayerPlugin ` for iOS: Provides type-safe access to AVPlayer instances
44
+ - ` RNVExoplayerPlugin ` for Android: Provides type-safe access to ExoPlayer instances
45
+
46
+ Choose the appropriate plugin type based on your needs. If you need direct access to player-specific APIs, use the player-specific plugin classes.
47
+
37
48
## Android Implementation
38
49
39
50
### 1. Create the Plugin
40
51
41
- First, instantiate a class that extends ` RNVPlugin ` .
52
+ You can implement either the base ` RNVPlugin ` interface or the player-specific ` RNVExoplayerPlugin ` interface.
53
+
54
+ #### Base Plugin
55
+
56
+ ``` kotlin
57
+ class MyAnalyticsPlugin : RNVPlugin {
58
+ override fun onInstanceCreated (id : String , player : Any ) {
59
+ // Handle player creation
60
+ }
61
+
62
+ override fun onInstanceRemoved (id : String , player : Any ) {
63
+ // Handle player removal
64
+ }
65
+ }
66
+ ```
67
+
68
+ #### ExoPlayer-Specific Plugin
42
69
43
- The recommended approach is to implement ` RNVPlugin ` inside the Module file (` VideoPluginSampleModule ` ).
70
+ ``` kotlin
71
+ class MyExoPlayerAnalyticsPlugin : RNVExoplayerPlugin {
72
+ override fun onInstanceCreated (id : String , player : ExoPlayer ) {
73
+ // Handle ExoPlayer creation with type-safe access
74
+ }
75
+
76
+ override fun onInstanceRemoved (id : String , player : ExoPlayer ) {
77
+ // Handle ExoPlayer removal with type-safe access
78
+ }
79
+ }
80
+ ```
44
81
45
82
The ` RNVPlugin ` interface defines two functions:
46
83
47
84
``` kotlin
48
85
/* *
49
- * Called when a new player instance is created.
50
- * @param id: A unique identifier for the player instance.
51
- * @param player: The instantiated player reference.
86
+ * Function called when a new player is created
87
+ * @param id: a random string identifying the player
88
+ * @param player: the instantiated player reference
52
89
*/
53
90
fun onInstanceCreated (id : String , player : Any )
54
91
55
92
/* *
56
- * Called when a player instance should be destroyed.
57
- * The plugin should free resources and release all references to the player object.
58
- * @param id: A unique identifier for the player instance.
59
- * @param player: The player to release.
93
+ * Function called when a player should be destroyed
94
+ * when this callback is called, the plugin shall free all
95
+ * resources and release all reference to Player object
96
+ * @param id: a random string identifying the player
97
+ * @param player: the player to release
60
98
*/
61
99
fun onInstanceRemoved (id : String , player : Any )
62
100
```
@@ -65,8 +103,6 @@ fun onInstanceRemoved(id: String, player: Any)
65
103
66
104
To register the plugin within the main ` react-native-video ` package, call:
67
105
68
- To register your plugin with the main react native video package, call the following function:
69
-
70
106
``` kotlin
71
107
ReactNativeVideoManager .getInstance().registerPlugin(plugin)
72
108
```
@@ -87,25 +123,54 @@ s.dependency "react-native-video"
87
123
88
124
### 2. Create the Plugin
89
125
90
- Instantiate a class that extends ` RNVPlugin ` .
126
+ You can implement either the base ` RNVPlugin ` class or the player-specific ` RNVAVPlayerPlugin ` class .
91
127
92
- The recommended approach is to implement ` RNVPlugin ` inside the entry point module file ( ` VideoPluginSample ` ).
128
+ #### Base Plugin
93
129
94
- The ` RNVPlugin ` interface defines two functions:
130
+ ``` swift
131
+ class MyAnalyticsPlugin : RNVPlugin {
132
+ override func onInstanceCreated (id : String , player : Any ) {
133
+ // Handle player creation
134
+ }
135
+
136
+ override func onInstanceRemoved (id : String , player : Any ) {
137
+ // Handle player removal
138
+ }
139
+ }
140
+ ```
141
+
142
+ #### AVPlayer-Specific Plugin
143
+
144
+ ``` swift
145
+ class MyAVPlayerAnalyticsPlugin : RNVAVPlayerPlugin {
146
+ override func onInstanceCreated (id : String , player : AVPlayer) {
147
+ // Handle AVPlayer creation with type-safe access
148
+ }
149
+
150
+ override func onInstanceRemoved (id : String , player : AVPlayer) {
151
+ // Handle AVPlayer removal with type-safe access
152
+ }
153
+ }
154
+ ```
155
+
156
+ The ` RNVPlugin ` class defines two methods:
95
157
96
158
``` swift
97
159
/**
98
- * Called when a new player instance is created.
99
- * @param player: The instantiated player reference.
160
+ * Function called when a new player is created
161
+ * @param id: a random string identifying the player
162
+ * @param player: the instantiated player reference
100
163
*/
101
- func onInstanceCreated (player : Any )
164
+ open func onInstanceCreated (id : String , player : Any ) { /* no-op */ }
102
165
103
166
/**
104
- * Called when a player instance should be destroyed.
105
- * The plugin should free resources and release all references to the player object.
106
- * @param player: The player to release.
167
+ * Function called when a player should be destroyed
168
+ * when this callback is called, the plugin shall free all
169
+ * resources and release all reference to Player object
170
+ * @param id: a random string identifying the player
171
+ * @param player: the player to release
107
172
*/
108
- func onInstanceRemoved (player : Any )
173
+ open func onInstanceRemoved (id : String , player : Any ) { /* no-op */ }
109
174
```
110
175
111
176
### 3. Register the Plugin
@@ -154,7 +219,7 @@ class CustomDRMManager : DRMManagerSpec {
154
219
155
220
#### 2/ Register DRM manager in your plugin
156
221
157
- Implement ` getDRMManager() ` in your plugin to provide the custom DRM manager:
222
+ Implement ` getDRMManager() ` in your ExoPlayer plugin to provide the custom DRM manager:
158
223
159
224
``` kotlin
160
225
class CustomVideoPlugin : RNVExoplayerPlugin {
@@ -216,12 +281,12 @@ class CustomDRMManager: NSObject, DRMManagerSpec {
216
281
217
282
#### 2/ Register DRM manager in your plugin
218
283
219
- Implement ` getDRMManager() ` in your plugin to provide the custom DRM manager:
284
+ Implement ` getDRMManager() ` in your AVPlayer plugin to provide the custom DRM manager:
220
285
221
286
``` swift
222
287
class CustomVideoPlugin : RNVAVPlayerPlugin {
223
288
override func getDRMManager () -> DRMManagerSpec? {
224
- return CustomDRMManager. self
289
+ return CustomDRMManager ()
225
290
}
226
291
227
292
override func onInstanceCreated (id : String , player : AVPlayer) {
0 commit comments