@jwplayer/jwplayer-react-native 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
-
2
1
  package com.jwplayer.rnjwplayer;
3
2
 
4
- import android.media.AudioManager;
3
+ import android.os.Handler;
4
+ import android.os.Looper;
5
5
 
6
6
  import com.facebook.react.bridge.Arguments;
7
7
  import com.facebook.react.bridge.Promise;
@@ -9,518 +9,415 @@ import com.facebook.react.bridge.ReactApplicationContext;
9
9
  import com.facebook.react.bridge.ReactContextBaseJavaModule;
10
10
  import com.facebook.react.bridge.ReactMethod;
11
11
  import com.facebook.react.bridge.ReadableArray;
12
+ import com.facebook.react.bridge.UIManager;
12
13
  import com.facebook.react.bridge.ReadableMap;
13
14
  import com.facebook.react.bridge.WritableArray;
14
15
  import com.facebook.react.bridge.WritableMap;
15
16
  import com.facebook.react.uimanager.IllegalViewOperationException;
16
17
  import com.facebook.react.uimanager.NativeViewHierarchyManager;
17
18
  import com.facebook.react.uimanager.UIBlock;
19
+ import com.facebook.react.uimanager.UIManagerHelper;
18
20
  import com.facebook.react.uimanager.UIManagerModule;
21
+ import com.facebook.react.uimanager.common.UIManagerType;
19
22
  import com.jwplayer.pub.api.JWPlayer;
20
23
  import com.jwplayer.pub.api.PlayerState;
21
- import com.jwplayer.pub.api.media.adaptive.QualityLevel;
22
24
  import com.jwplayer.pub.api.configuration.PlayerConfig;
25
+ import com.jwplayer.pub.api.media.adaptive.QualityLevel;
23
26
  import com.jwplayer.pub.api.media.audio.AudioTrack;
24
- import com.jwplayer.pub.api.media.playlists.PlaylistItem;
25
-
26
- import java.util.ArrayList;
27
27
  import java.util.List;
28
28
 
29
29
  public class RNJWPlayerModule extends ReactContextBaseJavaModule {
30
30
 
31
- private final ReactApplicationContext mReactContext;
32
-
33
- private static final String TAG = "RNJWPlayerModule";
34
-
35
- public RNJWPlayerModule(ReactApplicationContext reactContext) {
36
- super(reactContext);
37
-
38
- mReactContext = reactContext;
39
- }
40
-
41
- @Override
42
- public String getName() {
43
- return TAG;
44
- }
45
-
46
- @ReactMethod
47
- public void loadPlaylist(final int reactTag, final ReadableArray playlistItems) {
48
- try {
49
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
50
- uiManager.addUIBlock(new UIBlock() {
51
- public void execute (NativeViewHierarchyManager nvhm) {
52
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
53
-
54
- if (playerView != null && playerView.mPlayerView != null) {
55
- JWPlayer player = playerView.mPlayerView.getPlayer();
56
-
57
- PlayerConfig oldConfig = player.getConfig();
58
- PlayerConfig config = new PlayerConfig.Builder()
59
- .autostart(oldConfig.getAutostart())
60
- .nextUpOffset(oldConfig.getNextUpOffset())
61
- .repeat(oldConfig.getRepeat())
62
- .relatedConfig(oldConfig.getRelatedConfig())
63
- .displayDescription(oldConfig.getDisplayDescription())
64
- .displayTitle(oldConfig.getDisplayTitle())
65
- .advertisingConfig(oldConfig.getAdvertisingConfig())
66
- .stretching(oldConfig.getStretching())
67
- .uiConfig(oldConfig.getUiConfig())
68
- .playlist(Util.createPlaylist(playlistItems))
69
- .allowCrossProtocolRedirects(oldConfig.getAllowCrossProtocolRedirects())
70
- .preload(oldConfig.getPreload())
71
- .useTextureView(oldConfig.useTextureView())
72
- .thumbnailPreview(oldConfig.getThumbnailPreview())
73
- .mute(oldConfig.getMute())
74
- .build();
75
-
76
- player.setup(config);
77
- }
78
- }
79
- });
80
- } catch (IllegalViewOperationException e) {
81
- throw e;
31
+ private final ReactApplicationContext mReactContext;
32
+
33
+ private static final String TAG = "RNJWPlayerModule";
34
+
35
+ public RNJWPlayerModule(ReactApplicationContext reactContext) {
36
+ super(reactContext);
37
+
38
+ mReactContext = reactContext;
82
39
  }
83
- }
84
-
85
- @ReactMethod
86
- public void play(final int reactTag) {
87
- try {
88
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
89
- uiManager.addUIBlock(new UIBlock() {
90
- public void execute (NativeViewHierarchyManager nvhm) {
91
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
92
-
93
- if (playerView != null && playerView.mPlayerView != null) {
94
- playerView.mPlayerView.getPlayer().play();
95
- }
40
+
41
+ @Override
42
+ public String getName() {
43
+ return TAG;
44
+ }
45
+
46
+ private RNJWPlayerView getPlayerView(int reactTag) {
47
+ int uiManagerType;
48
+ if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
49
+ uiManagerType = UIManagerType.FABRIC;
50
+ } else {
51
+ uiManagerType = UIManagerType.DEFAULT;
52
+ }
53
+ UIManager uiManager = UIManagerHelper.getUIManager(mReactContext, uiManagerType);
54
+ if (uiManager != null) {
55
+ return (RNJWPlayerView) uiManager.resolveView(reactTag);
56
+ } else {
57
+ return null;
96
58
  }
97
- });
98
- } catch (IllegalViewOperationException e) {
99
- throw e;
100
59
  }
101
- }
102
-
103
- @ReactMethod
104
- public void toggleSpeed(final int reactTag) {
105
- try {
106
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
107
- uiManager.addUIBlock(new UIBlock() {
108
- public void execute (NativeViewHierarchyManager nvhm) {
109
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
110
-
111
- if (playerView != null && playerView.mPlayerView != null) {
112
- double rate = playerView.mPlayerView.getPlayer().getPlaybackRate();
113
- if (rate < 2) {
114
- playerView.mPlayerView.getPlayer().setPlaybackRate(rate += 0.5);
115
- } else {
116
- playerView.mPlayerView.getPlayer().setPlaybackRate((float) 0.5);
60
+
61
+ @ReactMethod
62
+ public void loadPlaylist(final int reactTag, final ReadableArray playlistItems) {
63
+ new Handler(Looper.getMainLooper()).post(() -> {
64
+ RNJWPlayerView playerView = getPlayerView(reactTag);
65
+ if (playerView != null && playerView.mPlayerView != null) {
66
+ JWPlayer player = playerView.mPlayerView.getPlayer();
67
+
68
+ PlayerConfig oldConfig = player.getConfig();
69
+ PlayerConfig config = new PlayerConfig.Builder()
70
+ .autostart(oldConfig.getAutostart())
71
+ .nextUpOffset(oldConfig.getNextUpOffset())
72
+ .repeat(oldConfig.getRepeat())
73
+ .relatedConfig(oldConfig.getRelatedConfig())
74
+ .displayDescription(oldConfig.getDisplayDescription())
75
+ .displayTitle(oldConfig.getDisplayTitle())
76
+ .advertisingConfig(oldConfig.getAdvertisingConfig())
77
+ .stretching(oldConfig.getStretching())
78
+ .uiConfig(oldConfig.getUiConfig())
79
+ .playlist(Util.createPlaylist(playlistItems))
80
+ .allowCrossProtocolRedirects(oldConfig.getAllowCrossProtocolRedirects())
81
+ .preload(oldConfig.getPreload())
82
+ .useTextureView(oldConfig.useTextureView())
83
+ .thumbnailPreview(oldConfig.getThumbnailPreview())
84
+ .mute(oldConfig.getMute())
85
+ .build();
86
+
87
+ player.setup(config);
117
88
  }
118
- }
119
- }
120
- });
121
- } catch (IllegalViewOperationException e) {
122
- throw e;
89
+ });
123
90
  }
124
- }
125
-
126
- @ReactMethod
127
- public void togglePIP(final int reactTag) {
128
- try {
129
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
130
- uiManager.addUIBlock(new UIBlock() {
131
- public void execute (NativeViewHierarchyManager nvhm) {
132
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
133
-
134
- if (playerView != null && playerView.mPlayerView != null) {
135
- if (playerView.mPlayerView.getPlayer().isInPictureInPictureMode()) {
136
- playerView.mPlayerView.getPlayer().exitPictureInPictureMode();
137
- } else {
138
- playerView.mPlayerView.getPlayer().enterPictureInPictureMode();
91
+
92
+ @ReactMethod
93
+ public void loadPlaylistWithUrl(final int reactTag, final String playlistUrl) {
94
+ new Handler(Looper.getMainLooper()).post(() -> {
95
+ RNJWPlayerView playerView = getPlayerView(reactTag);
96
+ if (playerView != null && playerView.mPlayerView != null) {
97
+ JWPlayer player = playerView.mPlayerView.getPlayer();
98
+
99
+ PlayerConfig oldConfig = player.getConfig();
100
+ PlayerConfig config = new PlayerConfig.Builder()
101
+ .autostart(oldConfig.getAutostart())
102
+ .nextUpOffset(oldConfig.getNextUpOffset())
103
+ .repeat(oldConfig.getRepeat())
104
+ .relatedConfig(oldConfig.getRelatedConfig())
105
+ .displayDescription(oldConfig.getDisplayDescription())
106
+ .displayTitle(oldConfig.getDisplayTitle())
107
+ .advertisingConfig(oldConfig.getAdvertisingConfig())
108
+ .stretching(oldConfig.getStretching())
109
+ .uiConfig(oldConfig.getUiConfig())
110
+ .playlistUrl(playlistUrl)
111
+ .allowCrossProtocolRedirects(oldConfig.getAllowCrossProtocolRedirects())
112
+ .preload(oldConfig.getPreload())
113
+ .useTextureView(oldConfig.useTextureView())
114
+ .thumbnailPreview(oldConfig.getThumbnailPreview())
115
+ .mute(oldConfig.getMute())
116
+ .build();
117
+
118
+ player.setup(config);
139
119
  }
140
- }
141
- }
142
- });
143
- } catch (IllegalViewOperationException e) {
144
- throw e;
120
+ });
145
121
  }
146
- }
147
-
148
- @ReactMethod
149
- public void setSpeed(final int reactTag, final float speed) {
150
- try {
151
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
152
- uiManager.addUIBlock(new UIBlock() {
153
- public void execute (NativeViewHierarchyManager nvhm) {
154
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
155
-
156
- if (playerView != null && playerView.mPlayerView != null) {
157
- playerView.mPlayerView.getPlayer().setPlaybackRate(speed);
158
- }
159
- }
160
- });
161
- } catch (IllegalViewOperationException e) {
162
- throw e;
122
+
123
+ @ReactMethod
124
+ public void play(final int reactTag) {
125
+ new Handler(Looper.getMainLooper()).post(() -> {
126
+ RNJWPlayerView playerView = getPlayerView(reactTag);
127
+ if (playerView != null && playerView.mPlayerView != null) {
128
+ playerView.mPlayerView.getPlayer().play();
129
+ }
130
+ });
163
131
  }
164
- }
165
-
166
-
167
- @ReactMethod
168
- public void getCurrentQuality(final int reactTag, final Promise promise) {
169
- try {
170
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
171
- uiManager.addUIBlock(new UIBlock() {
172
- public void execute (NativeViewHierarchyManager nvhm) {
173
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
174
-
175
- if (playerView != null && playerView.mPlayerView != null) {
176
- int quality = playerView.mPlayerView.getPlayer().getCurrentQuality();
177
- promise.resolve(quality);
178
- } else {
179
- promise.reject("RNJW Error", "getCurrentQuality() Player is null");
180
- }
181
- }
182
- });
183
- } catch (IllegalViewOperationException e) {
184
- throw e;
132
+
133
+ @ReactMethod
134
+ public void toggleSpeed(final int reactTag) {
135
+ new Handler(Looper.getMainLooper()).post(() -> {
136
+ RNJWPlayerView playerView = getPlayerView(reactTag);
137
+ if (playerView != null && playerView.mPlayerView != null) {
138
+ double rate = playerView.mPlayerView.getPlayer().getPlaybackRate();
139
+ if (rate < 2) {
140
+ playerView.mPlayerView.getPlayer().setPlaybackRate(rate += 0.5);
141
+ } else {
142
+ playerView.mPlayerView.getPlayer().setPlaybackRate((float) 0.5);
143
+ }
144
+ }
145
+ });
185
146
  }
186
- }
187
-
188
- @ReactMethod
189
- public void setCurrentQuality(final int reactTag, final int index) {
190
- try {
191
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
192
- uiManager.addUIBlock(new UIBlock() {
193
- public void execute (NativeViewHierarchyManager nvhm) {
194
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
195
-
196
- if (playerView != null && playerView.mPlayerView != null) {
197
- playerView.mPlayerView.getPlayer().setCurrentQuality(index);
198
- }
199
- }
200
- });
201
- } catch (IllegalViewOperationException e) {
202
- throw e;
147
+
148
+ @ReactMethod
149
+ public void togglePIP(final int reactTag) {
150
+ new Handler(Looper.getMainLooper()).post(() -> {
151
+ RNJWPlayerView playerView = getPlayerView(reactTag);
152
+ if (playerView != null && playerView.mPlayerView != null) {
153
+ if (playerView.mPlayerView.getPlayer().isInPictureInPictureMode()) {
154
+ playerView.mPlayerView.getPlayer().exitPictureInPictureMode();
155
+ } else {
156
+ playerView.mPlayerView.getPlayer().enterPictureInPictureMode();
157
+ }
158
+ }
159
+ });
203
160
  }
204
- }
205
-
206
-
207
- @ReactMethod
208
- public void getQualityLevels(final int reactTag, final Promise promise) {
209
- try {
210
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
211
- uiManager.addUIBlock(new UIBlock() {
212
- public void execute (NativeViewHierarchyManager nvhm) {
213
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
214
-
215
- if (playerView != null && playerView.mPlayerView != null) {
216
- List<QualityLevel> qualityLevelsList = playerView.mPlayerView.getPlayer().getQualityLevels();
217
- if (qualityLevelsList == null) { //if qualitylevels are null than pass empty array.
218
- promise.resolve(null);
219
- return;
161
+
162
+ @ReactMethod
163
+ public void setSpeed(final int reactTag, final float speed) {
164
+ new Handler(Looper.getMainLooper()).post(() -> {
165
+ RNJWPlayerView playerView = getPlayerView(reactTag);
166
+ if (playerView != null && playerView.mPlayerView != null) {
167
+ playerView.mPlayerView.getPlayer().setPlaybackRate(speed);
220
168
  }
221
- WritableArray qualityLevels = Arguments.createArray();
222
- for (int i = 0; i < qualityLevelsList.size(); i++) {
223
- WritableMap qualityLevel = Arguments.createMap();
224
- QualityLevel level = qualityLevelsList.get(i);
225
- qualityLevel.putInt("playListPosition", level.getPlaylistPosition());
226
- qualityLevel.putInt("bitRate", level.getBitrate());
227
- qualityLevel.putString("label", level.getLabel());
228
- qualityLevel.putInt("height", level.getHeight());
229
- qualityLevel.putInt("width", level.getWidth());
230
- qualityLevel.putInt("index", level.getTrackIndex());
231
- qualityLevels.pushMap(qualityLevel);
169
+ });
170
+ }
171
+
172
+ @ReactMethod
173
+ public void getCurrentQuality(final int reactTag, final Promise promise) {
174
+ new Handler(Looper.getMainLooper()).post(() -> {
175
+ RNJWPlayerView playerView = getPlayerView(reactTag);
176
+ if (playerView != null && playerView.mPlayerView != null) {
177
+ int quality = playerView.mPlayerView.getPlayer().getCurrentQuality();
178
+ promise.resolve(quality);
179
+ } else {
180
+ promise.reject("RNJW Error", "getCurrentQuality() Player is null");
232
181
  }
233
- promise.resolve(qualityLevels);
234
- } else {
235
- promise.reject("RNJW Error", "getQualityLevels() Player is null");
236
- }
237
- }
238
- });
239
- } catch (IllegalViewOperationException e) {
240
- throw e;
182
+ });
241
183
  }
242
- }
243
-
244
- @ReactMethod
245
- public void pause(final int reactTag) {
246
- try {
247
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
248
- uiManager.addUIBlock(new UIBlock() {
249
- public void execute (NativeViewHierarchyManager nvhm) {
250
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
251
-
252
- if (playerView != null && playerView.mPlayerView != null) {
253
- playerView.mPlayerView.getPlayer().pause();
254
- playerView.userPaused = true;
255
- }
256
- }
257
- });
258
- } catch (IllegalViewOperationException e) {
259
- throw e;
184
+
185
+ @ReactMethod
186
+ public void setCurrentQuality(final int reactTag, final int index) {
187
+ new Handler(Looper.getMainLooper()).post(() -> {
188
+ RNJWPlayerView playerView = getPlayerView(reactTag);
189
+ if (playerView != null && playerView.mPlayerView != null) {
190
+ playerView.mPlayerView.getPlayer().setCurrentQuality(index);
191
+ }
192
+ });
260
193
  }
261
- }
262
-
263
- @ReactMethod
264
- public void stop(final int reactTag) {
265
- try {
266
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
267
- uiManager.addUIBlock(new UIBlock() {
268
- public void execute (NativeViewHierarchyManager nvhm) {
269
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
270
-
271
- if (playerView != null && playerView.mPlayerView != null) {
272
- playerView.mPlayerView.getPlayer().stop();
273
- playerView.userPaused = true;
274
- }
275
- }
276
- });
277
- } catch (IllegalViewOperationException e) {
278
- throw e;
194
+
195
+ @ReactMethod
196
+ public void getQualityLevels(final int reactTag, final Promise promise) {
197
+ new Handler(Looper.getMainLooper()).post(() -> {
198
+ RNJWPlayerView playerView = getPlayerView(reactTag);
199
+ if (playerView != null && playerView.mPlayerView != null) {
200
+ List<QualityLevel> qualityLevelsList = playerView.mPlayerView.getPlayer().getQualityLevels();
201
+ if (qualityLevelsList == null) { //if qualitylevels are null than pass empty array.
202
+ promise.resolve(null);
203
+ return;
204
+ }
205
+ WritableArray qualityLevels = Arguments.createArray();
206
+ for (int i = 0; i < qualityLevelsList.size(); i++) {
207
+ WritableMap qualityLevel = Arguments.createMap();
208
+ QualityLevel level = qualityLevelsList.get(i);
209
+ qualityLevel.putInt("playListPosition", level.getPlaylistPosition());
210
+ qualityLevel.putInt("bitRate", level.getBitrate());
211
+ qualityLevel.putString("label", level.getLabel());
212
+ qualityLevel.putInt("height", level.getHeight());
213
+ qualityLevel.putInt("width", level.getWidth());
214
+ qualityLevel.putInt("index", level.getTrackIndex());
215
+ qualityLevels.pushMap(qualityLevel);
216
+ }
217
+ promise.resolve(qualityLevels);
218
+ } else {
219
+ promise.reject("RNJW Error", "getQualityLevels() Player is null");
220
+ }
221
+ });
279
222
  }
280
- }
281
-
282
- @ReactMethod
283
- public void seekTo(final int reactTag, final double time) {
284
- try {
285
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
286
- uiManager.addUIBlock(new UIBlock() {
287
- public void execute (NativeViewHierarchyManager nvhm) {
288
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
289
-
290
- if (playerView != null && playerView.mPlayerView != null) {
291
- playerView.mPlayerView.getPlayer().seek(time);
292
- }
293
- }
294
- });
295
- } catch (IllegalViewOperationException e) {
296
- throw e;
223
+
224
+ @ReactMethod
225
+ public void pause(final int reactTag) {
226
+ new Handler(Looper.getMainLooper()).post(() -> {
227
+ RNJWPlayerView playerView = getPlayerView(reactTag);
228
+ if (playerView != null && playerView.mPlayerView != null) {
229
+ if (!playerView.getIsCastActive()) {
230
+ playerView.mPlayerView.getPlayer().pause();
231
+ playerView.userPaused = true;
232
+ }
233
+ }
234
+ });
297
235
  }
298
- }
299
-
300
- @ReactMethod
301
- public void setPlaylistIndex(final int reactTag, final int index) {
302
- try {
303
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
304
- uiManager.addUIBlock(new UIBlock() {
305
- public void execute (NativeViewHierarchyManager nvhm) {
306
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
307
-
308
- if (playerView != null && playerView.mPlayerView != null) {
309
- playerView.mPlayerView.getPlayer().playlistItem(index);
310
- }
311
- }
312
- });
313
- } catch (IllegalViewOperationException e) {
314
- throw e;
236
+
237
+ @ReactMethod
238
+ public void stop(final int reactTag) {
239
+ new Handler(Looper.getMainLooper()).post(() -> {
240
+ RNJWPlayerView playerView = getPlayerView(reactTag);
241
+ if (playerView != null && playerView.mPlayerView != null) {
242
+ if (!playerView.getIsCastActive()) {
243
+ playerView.mPlayerView.getPlayer().stop();
244
+ playerView.userPaused = true;
245
+ }
246
+ }
247
+ });
315
248
  }
316
- }
317
-
318
- @ReactMethod
319
- public void setControls(final int reactTag, final boolean show) {
320
- try {
321
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
322
- uiManager.addUIBlock(new UIBlock() {
323
- public void execute (NativeViewHierarchyManager nvhm) {
324
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
325
-
326
- if (playerView != null && playerView.mPlayerView != null) {
327
- playerView.mPlayerView.getPlayer().setControls(show);
328
- }
329
- }
330
- });
331
- } catch (IllegalViewOperationException e) {
332
- throw e;
249
+
250
+ @ReactMethod
251
+ public void seekTo(final int reactTag, final double time) {
252
+ new Handler(Looper.getMainLooper()).post(() -> {
253
+ RNJWPlayerView playerView = getPlayerView(reactTag);
254
+ if (playerView != null && playerView.mPlayerView != null) {
255
+ playerView.mPlayerView.getPlayer().seek(time);
256
+ }
257
+ });
333
258
  }
334
- }
335
-
336
- @ReactMethod
337
- public void position(final int reactTag, final Promise promise) {
338
- try {
339
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
340
- uiManager.addUIBlock(new UIBlock() {
341
- public void execute (NativeViewHierarchyManager nvhm) {
342
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
343
-
344
- if (playerView != null && playerView.mPlayerView != null) {
345
- promise.resolve((Double.valueOf(playerView.mPlayerView.getPlayer().getPosition()).intValue()));
346
- } else {
347
- promise.reject("RNJW Error", "Player is null");
348
- }
349
- }
350
- });
351
- } catch (IllegalViewOperationException e) {
352
- promise.reject("RNJW Error", e);
259
+
260
+ @ReactMethod
261
+ public void setPlaylistIndex(final int reactTag, final int index) {
262
+ new Handler(Looper.getMainLooper()).post(() -> {
263
+ RNJWPlayerView playerView = getPlayerView(reactTag);
264
+ if (playerView != null && playerView.mPlayerView != null) {
265
+ playerView.mPlayerView.getPlayer().playlistItem(index);
266
+ }
267
+ });
353
268
  }
354
- }
355
-
356
- @ReactMethod
357
- public void state(final int reactTag, final Promise promise) {
358
- try {
359
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
360
- uiManager.addUIBlock(new UIBlock() {
361
- public void execute (NativeViewHierarchyManager nvhm) {
362
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
363
-
364
- if (playerView != null && playerView.mPlayerView != null) {
365
- PlayerState playerState = playerView.mPlayerView.getPlayer().getState();
366
- promise.resolve(stateToInt(playerState));
367
- } else {
368
- promise.reject("RNJW Error", "Player is null");
369
- }
370
- }
371
- });
372
- } catch (IllegalViewOperationException e) {
373
- promise.reject("RNJW Error", e);
269
+
270
+ @ReactMethod
271
+ public void setControls(final int reactTag, final boolean show) {
272
+ new Handler(Looper.getMainLooper()).post(() -> {
273
+ RNJWPlayerView playerView = getPlayerView(reactTag);
274
+ if (playerView != null && playerView.mPlayerView != null) {
275
+ playerView.mPlayerView.getPlayer().setControls(show);
276
+ }
277
+ });
374
278
  }
375
- }
376
-
377
- @ReactMethod
378
- public void setFullscreen(final int reactTag, final boolean fullscreen) {
379
- try {
380
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
381
- uiManager.addUIBlock(new UIBlock() {
382
- public void execute (NativeViewHierarchyManager nvhm) {
383
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
384
-
385
- if (playerView != null && playerView.mPlayerView != null) {
386
- playerView.mPlayerView.getPlayer().setFullscreen(fullscreen, fullscreen);
387
- }
388
- }
389
- });
390
- } catch (IllegalViewOperationException e) {
391
- throw e;
279
+
280
+ @ReactMethod
281
+ public void position(final int reactTag, final Promise promise) {
282
+ new Handler(Looper.getMainLooper()).post(() -> {
283
+ RNJWPlayerView playerView = getPlayerView(reactTag);
284
+ if (playerView != null && playerView.mPlayerView != null) {
285
+ promise.resolve((Double.valueOf(playerView.mPlayerView.getPlayer().getPosition()).intValue()));
286
+ } else {
287
+ promise.reject("RNJW Error", "Player is null");
288
+ }
289
+ });
392
290
  }
393
- }
394
-
395
- @ReactMethod
396
- public void setVolume(final int reactTag, final int volume) {
397
- try {
398
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
399
- uiManager.addUIBlock(new UIBlock() {
400
- public void execute (NativeViewHierarchyManager nvhm) {
401
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
402
-
403
- if (playerView != null && playerView.mPlayerView != null) {
404
- int maxValue = playerView.audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
405
- if (volume <= maxValue) {
406
- playerView.audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
291
+
292
+ @ReactMethod
293
+ public void state(final int reactTag, final Promise promise) {
294
+ new Handler(Looper.getMainLooper()).post(() -> {
295
+ RNJWPlayerView playerView = getPlayerView(reactTag);
296
+ if (playerView != null && playerView.mPlayerView != null) {
297
+ PlayerState playerState = playerView.mPlayerView.getPlayer().getState();
298
+ promise.resolve(stateToInt(playerState));
407
299
  } else {
408
- playerView.audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxValue, 0);
300
+ promise.reject("RNJW Error", "Player is null");
409
301
  }
410
- }
411
- }
412
- });
413
- } catch (IllegalViewOperationException e) {
414
- throw e;
302
+ });
415
303
  }
416
- }
417
-
418
- @ReactMethod
419
- public void getAudioTracks(final int reactTag, final Promise promise) {
420
- try {
421
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
422
- uiManager.addUIBlock(new UIBlock() {
423
- public void execute (NativeViewHierarchyManager nvhm) {
424
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
425
-
426
- if (playerView != null && playerView.mPlayer != null) {
427
- List<AudioTrack> audioTrackList = playerView.mPlayer.getAudioTracks();
428
- WritableArray audioTracks = Arguments.createArray();
429
- if (audioTrackList != null) {
430
- for (int i = 0; i < audioTrackList.size(); i++) {
431
- WritableMap audioTrack = Arguments.createMap();
432
- AudioTrack track = audioTrackList.get(i);
433
- audioTrack.putString("name", track.getName());
434
- audioTrack.putString("language", track.getLanguage());
435
- audioTrack.putString("groupId", track.getGroupId());
436
- audioTrack.putBoolean("defaultTrack", track.isDefaultTrack());
437
- audioTrack.putBoolean("autoSelect", track.isAutoSelect());
438
- audioTracks.pushMap(audioTrack);
439
- }
304
+
305
+ @ReactMethod
306
+ public void setFullscreen(final int reactTag, final boolean fullscreen) {
307
+ new Handler(Looper.getMainLooper()).post(() -> {
308
+ RNJWPlayerView playerView = getPlayerView(reactTag);
309
+ if (playerView != null && playerView.mPlayerView != null) {
310
+ playerView.mPlayerView.getPlayer().setFullscreen(fullscreen, fullscreen);
440
311
  }
441
- promise.resolve(audioTracks);
442
- } else {
443
- promise.reject("RNJW Error", "Player is null");
444
- }
445
- }
446
- });
447
- } catch (IllegalViewOperationException e) {
448
- promise.reject("RNJW Error", e);
312
+ });
449
313
  }
450
- }
451
-
452
- @ReactMethod
453
- public void getCurrentAudioTrack(final int reactTag, final Promise promise) {
454
- try {
455
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
456
- uiManager.addUIBlock(new UIBlock() {
457
- public void execute (NativeViewHierarchyManager nvhm) {
458
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
459
-
460
- if (playerView != null && playerView.mPlayer != null) {
461
- promise.resolve(playerView.mPlayer.getCurrentAudioTrack());
462
- } else {
463
- promise.reject("RNJW Error", "Player is null");
464
- }
465
- }
466
- });
467
- } catch (IllegalViewOperationException e) {
468
- promise.reject("RNJW Error", e);
314
+
315
+ @ReactMethod
316
+ public void setVolume(final int reactTag, final int volume) {
317
+ new Handler(Looper.getMainLooper()).post(() -> {
318
+ RNJWPlayerView playerView = getPlayerView(reactTag);
319
+ if (playerView != null && playerView.mPlayerView != null) {
320
+ playerView.mPlayerView.getPlayer().setVolume(volume);
321
+ }
322
+ });
469
323
  }
470
- }
471
-
472
- @ReactMethod
473
- public void setCurrentAudioTrack(final int reactTag, final int index) {
474
- try {
475
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
476
- uiManager.addUIBlock(new UIBlock() {
477
- public void execute (NativeViewHierarchyManager nvhm) {
478
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
479
-
480
- if (playerView != null && playerView.mPlayer != null) {
481
- playerView.mPlayer.setCurrentAudioTrack(index);
482
- }
483
- }
484
- });
485
- } catch (IllegalViewOperationException e) {
486
- throw e;
324
+
325
+ @ReactMethod
326
+ public void getAudioTracks(final int reactTag, final Promise promise) {
327
+ new Handler(Looper.getMainLooper()).post(() -> {
328
+ RNJWPlayerView playerView = getPlayerView(reactTag);
329
+ if (playerView != null && playerView.mPlayer != null) {
330
+ List<AudioTrack> audioTrackList = playerView.mPlayer.getAudioTracks();
331
+ WritableArray audioTracks = Arguments.createArray();
332
+ if (audioTrackList != null) {
333
+ for (int i = 0; i < audioTrackList.size(); i++) {
334
+ WritableMap audioTrack = Arguments.createMap();
335
+ AudioTrack track = audioTrackList.get(i);
336
+ audioTrack.putString("name", track.getName());
337
+ audioTrack.putString("language", track.getLanguage());
338
+ audioTrack.putString("groupId", track.getGroupId());
339
+ audioTrack.putBoolean("defaultTrack", track.isDefaultTrack());
340
+ audioTrack.putBoolean("autoSelect", track.isAutoSelect());
341
+ audioTracks.pushMap(audioTrack);
342
+ }
343
+ }
344
+ promise.resolve(audioTracks);
345
+ } else {
346
+ promise.reject("RNJW Error", "Player is null");
347
+ }
348
+ });
487
349
  }
488
- }
489
-
490
- @ReactMethod
491
- public void setCurrentCaptions(final int reactTag, final int index) {
492
- try {
493
- UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
494
- uiManager.addUIBlock(new UIBlock() {
495
- public void execute (NativeViewHierarchyManager nvhm) {
496
- RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
497
-
498
- if (playerView != null && playerView.mPlayer != null) {
499
- playerView.mPlayer.setCurrentCaptions(index);
500
- }
501
- }
502
- });
503
- } catch (IllegalViewOperationException e) {
504
- throw e;
350
+
351
+ @ReactMethod
352
+ public void getCurrentAudioTrack(final int reactTag, final Promise promise) {
353
+ new Handler(Looper.getMainLooper()).post(() -> {
354
+ RNJWPlayerView playerView = getPlayerView(reactTag);
355
+ if (playerView != null && playerView.mPlayer != null) {
356
+ promise.resolve(playerView.mPlayer.getCurrentAudioTrack());
357
+ } else {
358
+ promise.reject("RNJW Error", "Player is null");
359
+ }
360
+ });
361
+ }
362
+
363
+ @ReactMethod
364
+ public void setCurrentAudioTrack(final int reactTag, final int index) {
365
+ new Handler(Looper.getMainLooper()).post(() -> {
366
+ RNJWPlayerView playerView = getPlayerView(reactTag);
367
+ if (playerView != null && playerView.mPlayer != null) {
368
+ playerView.mPlayer.setCurrentAudioTrack(index);
369
+ }
370
+ });
371
+ }
372
+
373
+ @ReactMethod
374
+ public void setCurrentCaptions(final int reactTag, final int index) {
375
+ new Handler(Looper.getMainLooper()).post(() -> {
376
+ RNJWPlayerView playerView = getPlayerView(reactTag);
377
+ if (playerView != null && playerView.mPlayer != null) {
378
+ playerView.mPlayer.setCurrentCaptions(index);
379
+ }
380
+ });
381
+ }
382
+
383
+ @ReactMethod
384
+ public void getCurrentCaptions(final int reactTag, final Promise promise) {
385
+ new Handler(Looper.getMainLooper()).post(() -> {
386
+ RNJWPlayerView playerView = getPlayerView(reactTag);
387
+ if (playerView != null && playerView.mPlayer != null) {
388
+ promise.resolve(playerView.mPlayer.getCurrentCaptions());
389
+ } else {
390
+ promise.reject("RNJW Error", "Player is null");
391
+ }
392
+ });
505
393
  }
506
- }
507
-
508
- private int stateToInt(PlayerState playerState) {
509
- switch (playerState) {
510
- case IDLE:
511
- return 0;
512
- case BUFFERING:
513
- return 1;
514
- case PLAYING:
515
- return 2;
516
- case PAUSED:
517
- return 3;
518
- case COMPLETE:
519
- return 4;
520
- case ERROR:
521
- return 5;
522
- default:
523
- return -1;
394
+
395
+ @ReactMethod
396
+ public void resolveNextPlaylistItem(final int reactTag, final ReadableMap playlistItem) {
397
+ new Handler(Looper.getMainLooper()).post(() -> {
398
+ RNJWPlayerView playerView = getPlayerView(reactTag);
399
+ if (playerView != null && playerView.mPlayerView != null) {
400
+ playerView.resolveNextPlaylistItem(playlistItem);
401
+ }
402
+ });
403
+ }
404
+
405
+ private int stateToInt(PlayerState playerState) {
406
+ switch (playerState) {
407
+ case IDLE:
408
+ return 0;
409
+ case BUFFERING:
410
+ return 1;
411
+ case PLAYING:
412
+ return 2;
413
+ case PAUSED:
414
+ return 3;
415
+ case COMPLETE:
416
+ return 4;
417
+ case ERROR:
418
+ return 5;
419
+ default:
420
+ return -1;
421
+ }
524
422
  }
525
- }
526
423
  }