Skip to content
This repository was archived by the owner on May 16, 2024. It is now read-only.

Commit 7133c96

Browse files
authored
fix: ensure progress is sent before onEnd callback (TheWidlarzGroup#3872)
* fix: add onProgress event before onEnd
1 parent adedc05 commit 7133c96

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

android/src/main/java/com/brentvatne/common/api/Source.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ class Source {
5757
}
5858

5959
/** return true if this and src are equals */
60-
fun isEquals(source: Source): Boolean {
61-
return this == source
62-
}
60+
fun isEquals(source: Source): Boolean = this == source
6361

6462
/** Metadata to display in notification */
6563
class Metadata {

android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -258,29 +258,36 @@ public class ReactExoplayerView extends FrameLayout implements
258258
private long lastBufferDuration = -1;
259259
private long lastDuration = -1;
260260

261+
private void updateProgress() {
262+
if (player != null) {
263+
if (playerControlView != null && isPlayingAd() && controls) {
264+
playerControlView.hide();
265+
}
266+
long bufferedDuration = player.getBufferedPercentage() * player.getDuration() / 100;
267+
long duration = player.getDuration();
268+
long pos = player.getCurrentPosition();
269+
if (pos > duration) {
270+
pos = duration;
271+
}
272+
273+
if (lastPos != pos
274+
|| lastBufferDuration != bufferedDuration
275+
|| lastDuration != duration) {
276+
lastPos = pos;
277+
lastBufferDuration = bufferedDuration;
278+
lastDuration = duration;
279+
eventEmitter.progressChanged(pos, bufferedDuration, player.getDuration(), getPositionInFirstPeriodMsForCurrentWindow(pos));
280+
}
281+
}
282+
}
283+
261284
private final Handler progressHandler = new Handler(Looper.getMainLooper()) {
262285
@Override
263286
public void handleMessage(Message msg) {
264287
if (msg.what == SHOW_PROGRESS) {
265-
if (player != null) {
266-
if (playerControlView != null && isPlayingAd() && controls) {
267-
playerControlView.hide();
268-
}
269-
long pos = player.getCurrentPosition();
270-
long bufferedDuration = player.getBufferedPercentage() * player.getDuration() / 100;
271-
long duration = player.getDuration();
272-
273-
if (lastPos != pos
274-
|| lastBufferDuration != bufferedDuration
275-
|| lastDuration != duration) {
276-
lastPos = pos;
277-
lastBufferDuration = bufferedDuration;
278-
lastDuration = duration;
279-
eventEmitter.progressChanged(pos, bufferedDuration, player.getDuration(), getPositionInFirstPeriodMsForCurrentWindow(pos));
280-
}
281-
msg = obtainMessage(SHOW_PROGRESS);
282-
sendMessageDelayed(msg, Math.round(mProgressUpdateInterval));
283-
}
288+
updateProgress();
289+
msg = obtainMessage(SHOW_PROGRESS);
290+
sendMessageDelayed(msg, Math.round(mProgressUpdateInterval));
284291
}
285292
}
286293
};
@@ -1337,6 +1344,7 @@ public void onEvents(@NonNull Player player, Player.Events events) {
13371344
break;
13381345
case Player.STATE_ENDED:
13391346
text += "ended";
1347+
updateProgress();
13401348
eventEmitter.end();
13411349
onStopPlayback();
13421350
setKeepScreenOn(false);
@@ -1614,6 +1622,7 @@ public void onPositionDiscontinuity(@NonNull Player.PositionInfo oldPosition, @N
16141622
// so we need to explicitly detect it.
16151623
if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION
16161624
&& player.getRepeatMode() == Player.REPEAT_MODE_ONE) {
1625+
updateProgress();
16171626
eventEmitter.end();
16181627
}
16191628
}

ios/Video/RCTVideo.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
312312

313313
// MARK: - Progress
314314

315-
func sendProgressUpdate() {
315+
func sendProgressUpdate(didEnd: Bool = false) {
316316
#if !USE_GOOGLE_IMA
317317
// If we dont use Ads and onVideoProgress is not defined we dont need to run this code
318318
guard onVideoProgress != nil else { return }
@@ -334,11 +334,11 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
334334
}
335335
let currentPlaybackTime = _player?.currentItem?.currentDate()
336336
let duration = CMTimeGetSeconds(playerDuration)
337-
let currentTimeSecs = CMTimeGetSeconds(currentTime ?? .zero)
337+
var currentTimeSecs = CMTimeGetSeconds(currentTime ?? .zero)
338338

339-
NotificationCenter.default.post(name: NSNotification.Name("RCTVideo_progress"), object: nil, userInfo: [
340-
"progress": NSNumber(value: currentTimeSecs / duration),
341-
])
339+
if currentTimeSecs > duration || didEnd {
340+
currentTimeSecs = duration
341+
}
342342

343343
if currentTimeSecs >= 0 {
344344
#if USE_GOOGLE_IMA
@@ -348,10 +348,10 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
348348
}
349349
#endif
350350
onVideoProgress?([
351-
"currentTime": NSNumber(value: Float(currentTimeSecs)),
351+
"currentTime": currentTimeSecs,
352352
"playableDuration": RCTVideoUtils.calculatePlayableDuration(_player, withSource: _source),
353-
"atValue": NSNumber(value: currentTime?.value ?? .zero),
354-
"currentPlaybackTime": NSNumber(value: NSNumber(value: Double(currentPlaybackTime?.timeIntervalSince1970 ?? 0 * 1000)).int64Value),
353+
"atValue": currentTime?.value ?? .zero,
354+
"currentPlaybackTime": NSNumber(value: Double(currentPlaybackTime?.timeIntervalSince1970 ?? 0 * 1000)).int64Value,
355355
"target": reactTag,
356356
"seekableDuration": RCTVideoUtils.calculateSeekableDuration(_player),
357357
])
@@ -1570,6 +1570,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
15701570

15711571
@objc
15721572
func handlePlayerItemDidReachEnd(notification: NSNotification!) {
1573+
sendProgressUpdate(didEnd: true)
15731574
onVideoEnd?(["target": reactTag as Any])
15741575
#if USE_GOOGLE_IMA
15751576
if notification.object as? AVPlayerItem == _player?.currentItem {

0 commit comments

Comments
 (0)