Skip to content

Commit 2334cf2

Browse files
authored
[BUG] Previous video gets played on the Chromecast device (#87)
* make view lifecycle aware. pass lifecycle to player object. Fix cast bug * Format RNJWPlayerModule.java * Fix bad merge. Lock cast session to player. Update Example to include ima/cast * remove the skeleton code from path untaken
1 parent 98931c3 commit 2334cf2

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

Example/android/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ buildscript {
88
targetSdkVersion = 33
99
ndkVersion = "20.1.5948944"
1010
kotlinVersion = "1.8.0"
11+
RNJWPlayerUseGoogleCast = true
12+
RNJWPlayerUseGoogleIMA = true
1113
}
1214
repositories {
1315
google()

android/src/main/java/com/jwplayer/rnjwplayer/RNJWPlayerModule.java

+30-31
Original file line numberDiff line numberDiff line change
@@ -80,39 +80,38 @@ public void execute(NativeViewHierarchyManager nvhm) {
8080
@ReactMethod
8181
public void loadPlaylist(final int reactTag, final String playlistUrl) {
8282
try {
83-
UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
84-
uiManager.addUIBlock(new UIBlock() {
85-
public void execute (NativeViewHierarchyManager nvhm) {
86-
RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
83+
UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
84+
uiManager.addUIBlock(new UIBlock() {
85+
public void execute(NativeViewHierarchyManager nvhm) {
86+
RNJWPlayerView playerView = (RNJWPlayerView) nvhm.resolveView(reactTag);
87+
if (playerView != null && playerView.mPlayerView != null) {
88+
JWPlayer player = playerView.mPlayerView.getPlayer();
8789

88-
if (playerView != null && playerView.mPlayerView != null) {
89-
JWPlayer player = playerView.mPlayerView.getPlayer();
90-
91-
PlayerConfig oldConfig = player.getConfig();
92-
PlayerConfig config = new PlayerConfig.Builder()
93-
.autostart(oldConfig.getAutostart())
94-
.nextUpOffset(oldConfig.getNextUpOffset())
95-
.repeat(oldConfig.getRepeat())
96-
.relatedConfig(oldConfig.getRelatedConfig())
97-
.displayDescription(oldConfig.getDisplayDescription())
98-
.displayTitle(oldConfig.getDisplayTitle())
99-
.advertisingConfig(oldConfig.getAdvertisingConfig())
100-
.stretching(oldConfig.getStretching())
101-
.uiConfig(oldConfig.getUiConfig())
102-
.playlistUrl(playlistUrl)
103-
.allowCrossProtocolRedirects(oldConfig.getAllowCrossProtocolRedirects())
104-
.preload(oldConfig.getPreload())
105-
.useTextureView(oldConfig.useTextureView())
106-
.thumbnailPreview(oldConfig.getThumbnailPreview())
107-
.mute(oldConfig.getMute())
108-
.build();
109-
110-
player.setup(config);
111-
}
112-
}
113-
});
90+
PlayerConfig oldConfig = player.getConfig();
91+
PlayerConfig config = new PlayerConfig.Builder()
92+
.autostart(oldConfig.getAutostart())
93+
.nextUpOffset(oldConfig.getNextUpOffset())
94+
.repeat(oldConfig.getRepeat())
95+
.relatedConfig(oldConfig.getRelatedConfig())
96+
.displayDescription(oldConfig.getDisplayDescription())
97+
.displayTitle(oldConfig.getDisplayTitle())
98+
.advertisingConfig(oldConfig.getAdvertisingConfig())
99+
.stretching(oldConfig.getStretching())
100+
.uiConfig(oldConfig.getUiConfig())
101+
.playlistUrl(playlistUrl)
102+
.allowCrossProtocolRedirects(oldConfig.getAllowCrossProtocolRedirects())
103+
.preload(oldConfig.getPreload())
104+
.useTextureView(oldConfig.useTextureView())
105+
.thumbnailPreview(oldConfig.getThumbnailPreview())
106+
.mute(oldConfig.getMute())
107+
.build();
108+
109+
player.setup(config);
110+
}
111+
}
112+
});
114113
} catch (IllegalViewOperationException e) {
115-
throw e;
114+
throw e;
116115
}
117116
}
118117

android/src/main/java/com/jwplayer/rnjwplayer/RNJWPlayerView.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,13 @@ public void destroyPlayer() {
365365
if (mPlayer != null) {
366366
unRegisterReceiver();
367367

368-
// Only call stop if we are not casting as stop will break the current cast session
369-
if (!mIsCastActive) {
370-
mPlayer.stop();
371-
}
368+
// If we are casting we need to break the cast session as there is no simple
369+
// way to reconnect to an existing session if the player that created it is dead
370+
371+
// If this doesn't match your use case, using a single player object and load content
372+
// into it rather than creating a new player for every piece of content.
373+
mPlayer.stop();
374+
372375
// send signal to JW SDK player is destroyed
373376
registry.setCurrentState(Lifecycle.State.DESTROYED);
374377

0 commit comments

Comments
 (0)