-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gh 101 implement set playlist item callback #128
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,7 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
public class RNJWPlayerView extends RelativeLayout implements | ||
VideoPlayerEvents.OnFullscreenListener, | ||
|
@@ -156,6 +157,7 @@ public class RNJWPlayerView extends RelativeLayout implements | |
VideoPlayerEvents.OnCaptionsListListener, | ||
VideoPlayerEvents.OnCaptionsChangedListener, | ||
VideoPlayerEvents.OnMetaListener, | ||
VideoPlayerEvents.PlaylistItemCallbackListener, | ||
|
||
CastingEvents.OnCastListener, | ||
|
||
|
@@ -236,6 +238,9 @@ public class RNJWPlayerView extends RelativeLayout implements | |
private MediaServiceController mMediaServiceController; | ||
private PipHandlerReceiver mReceiver = null; | ||
|
||
// Add completion handler field | ||
PlaylistItemDecision itemUpdatePromise = null; | ||
|
||
private void doBindService() { | ||
if (mMediaServiceController != null) { | ||
if (!isBackgroundAudioServiceRunning()) { | ||
|
@@ -521,9 +526,30 @@ public void setupPlayerView(Boolean backgroundAudioEnabled) { | |
} else { | ||
mPlayer.setFullscreenHandler(new fullscreenHandler()); | ||
} | ||
|
||
mPlayer.allowBackgroundAudio(backgroundAudioEnabled); | ||
mPlayer.setPlaylistItemCallbackListener(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @normtronics this requires all lib. users to use it's not big of a deal on ios but its safer/cleaner if the same logic is applied as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove this listener when the player is destroyed as well - https://github.com/jwplayer/jwplayer-react-native/blob/master/android/src/main/java/com/jwplayer/rnjwplayer/RNJWPlayerView.java#L364 |
||
} | ||
} | ||
|
||
public void resolveNextPlaylistItem(ReadableMap playlistItem) { | ||
if (itemUpdatePromise == null) { | ||
return; | ||
} | ||
|
||
if (playlistItem == null) { | ||
itemUpdatePromise.continuePlayback(); | ||
itemUpdatePromise = null; | ||
return; | ||
} | ||
|
||
try { | ||
PlaylistItem updatedPlaylistItem = Util.getPlaylistItem(playlistItem); | ||
itemUpdatePromise.modify(updatedPlaylistItem); | ||
} catch (Exception exception) { | ||
itemUpdatePromise.continuePlayback(); | ||
} | ||
|
||
itemUpdatePromise = null; | ||
} | ||
|
||
/** | ||
|
@@ -627,6 +653,18 @@ public void onAllowFullscreenPortrait(boolean allowFullscreenPortrait) { | |
return delegate; | ||
} | ||
|
||
@Override | ||
public void onBeforeNextPlaylistItem(PlaylistItemDecision PlaylistItemDecision, PlaylistItem nextItem, int indexOfNextItem) { | ||
WritableMap event = Arguments.createMap(); | ||
Gson gson = new Gson(); | ||
event.putString("message", "onBeforeNextPlaylistItem"); | ||
event.putInt("index", indexOfNextItem); | ||
event.putString("playlistItem", gson.toJson(nextItem)); | ||
getReactContext().getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "topBeforeNextPlaylistItem", event); | ||
|
||
itemUpdatePromise = PlaylistItemDecision; | ||
} | ||
|
||
private class fullscreenHandler implements FullscreenHandler { | ||
ViewGroup mPlayerViewContainer = (ViewGroup) mPlayerView.getParent(); | ||
private View mDecorView; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ class RNJWPlayerView : UIView, JWPlayerDelegate, JWPlayerStateDelegate, JWAdDele | |
var castController: JWCastController! | ||
var isCasting: Bool = false | ||
var availableDevices: [AnyObject]! | ||
var onBeforeNextPlaylistItemCompletion: ((JWPlayerItem?) -> ())? | ||
|
||
@objc var onBuffer: RCTDirectEventBlock? | ||
@objc var onUpdateBuffer: RCTDirectEventBlock? | ||
|
@@ -87,6 +88,7 @@ class RNJWPlayerView : UIView, JWPlayerDelegate, JWPlayerStateDelegate, JWAdDele | |
@objc var onCastingFailed: RCTDirectEventBlock? | ||
@objc var onCaptionsChanged: RCTDirectEventBlock? | ||
@objc var onCaptionsList: RCTDirectEventBlock? | ||
@objc var onBeforeNextPlaylistItem: RCTDirectEventBlock? | ||
|
||
init() { | ||
super.init(frame: CGRect(x: 20, y: 0, width: UIScreen.main.bounds.width - 40, height: 300)) | ||
|
@@ -348,6 +350,8 @@ class RNJWPlayerView : UIView, JWPlayerDelegate, JWPlayerStateDelegate, JWAdDele | |
self.setupPlayerViewController(config: config, playerConfig: jwConfig!) | ||
} | ||
} | ||
|
||
self.setupPlaylistItemCallback() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @normtronics same idea on android. please set only when |
||
} catch { | ||
print(error) | ||
} | ||
|
@@ -362,6 +366,47 @@ class RNJWPlayerView : UIView, JWPlayerDelegate, JWPlayerStateDelegate, JWAdDele | |
} | ||
} | ||
|
||
func setupPlaylistItemCallback() { | ||
playerViewController.player.setPlaylistItemCallback { [weak self] item, index, completion in | ||
print("setPlaylistItemCallback called with index \(index)") | ||
guard let self = self else { | ||
print("setPlaylistItemCallback: self is nil, resuming normally") | ||
completion(item) | ||
return | ||
} | ||
|
||
if let onBeforeNextPlaylistItem = self.onBeforeNextPlaylistItem { | ||
print("Storing completion handler and triggering onBeforeNextPlaylistItem") | ||
// Store the completion handler first, before any other operations | ||
self.onBeforeNextPlaylistItemCompletion = completion | ||
print("Completion handler stored: \(self.onBeforeNextPlaylistItemCompletion != nil)") | ||
|
||
do { | ||
let data = try JSONSerialization.data(withJSONObject: item.toJSONObject(), options: [.prettyPrinted]) | ||
let jsonString = String(data: data, encoding: .utf8) ?? "{}" | ||
|
||
print("Triggering onBeforeNextPlaylistItem with index \(index)") | ||
print("Completion handler before event: \(self.onBeforeNextPlaylistItemCompletion != nil)") | ||
|
||
// Pass the playlist item to the React Native side | ||
onBeforeNextPlaylistItem([ | ||
"playlistItem": jsonString, | ||
"index": index | ||
]) | ||
print("Completion handler after event: \(self.onBeforeNextPlaylistItemCompletion != nil)") | ||
|
||
} catch { | ||
print("Error serializing playlist item: \(error)") | ||
self.onBeforeNextPlaylistItemCompletion?(item) // Call completion handler directly on error | ||
self.onBeforeNextPlaylistItemCompletion = nil | ||
} | ||
} else { | ||
print("No onBeforeNextPlaylistItem handler set, calling completion directly") | ||
completion(item) | ||
} | ||
} | ||
} | ||
|
||
// MARK: - RNJWPlayer styling | ||
|
||
func colorWithHexString(hex:String!) -> UIColor! { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@normtronics not needed.