From 4eb7d0f1516d221fb2a8afba450056fc4ab8de69 Mon Sep 17 00:00:00 2001 From: Alex Kim Date: Sun, 3 Nov 2024 23:17:54 +1100 Subject: [PATCH] Fixed skip intro skipping more than the video length --- components/video-player/Controls.tsx | 4 +++ hooks/useCreditSkipper.ts | 1 + modules/vlc-player/ios/VlcPlayerView.swift | 39 +++++++++++----------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/components/video-player/Controls.tsx b/components/video-player/Controls.tsx index a75163da..41a8db54 100644 --- a/components/video-player/Controls.tsx +++ b/components/video-player/Controls.tsx @@ -208,9 +208,12 @@ export const Controls: React.FC = ({ ? maxValue - currentProgress : ticksToSeconds(maxValue - currentProgress); + // console.log("Remaning time is: ", remaining); + setCurrentTime(current); setRemainingTime(remaining); + // Currently doesm't work in VLC because of some corrupted timestamps, will need to find a workaround. if (currentProgress === maxValue) { setShowControls(true); // Automatically play the next item if it exists @@ -227,6 +230,7 @@ export const Controls: React.FC = ({ isSeeking: isSeeking.value, }), (result) => { + // console.log("Progress changed", result); if (result.isSeeking === false) { runOnJS(updateTimes)(result.progress, result.max); } diff --git a/hooks/useCreditSkipper.ts b/hooks/useCreditSkipper.ts index 0ad52a56..5f7a8b34 100644 --- a/hooks/useCreditSkipper.ts +++ b/hooks/useCreditSkipper.ts @@ -76,6 +76,7 @@ export const useCreditSkipper = ( const skipCredit = useCallback(() => { if (!creditTimestamps) return; + console.log(`Skipping credits to ${creditTimestamps.Credits.End}`); try { wrappedSeek(creditTimestamps.Credits.End); setTimeout(() => { diff --git a/modules/vlc-player/ios/VlcPlayerView.swift b/modules/vlc-player/ios/VlcPlayerView.swift index 83a6a409..84ad55a5 100644 --- a/modules/vlc-player/ios/VlcPlayerView.swift +++ b/modules/vlc-player/ios/VlcPlayerView.swift @@ -78,14 +78,22 @@ class VlcPlayerView: ExpoView { player.pause() } - player.time = VLCTime(int: time) + if let duration = player.media?.length.intValue { + print("Seeking to time: \(time) Video Duration \(duration)") - // Wait for a short moment to ensure the seek has been processed - DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { - if wasPlaying { - player.play() + // If the specified time is greater than the duration, seek to the end + let seekTime = time > duration ? duration - 1000 : time + player.time = VLCTime(int: seekTime) + + // Wait for a short moment to ensure the seek has been processed + DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { + if wasPlaying { + player.play() + } + self.updatePlayerState() } - self.updatePlayerState() + } else { + print("Error: Unable to retrieve video duration") } } } @@ -94,7 +102,7 @@ class VlcPlayerView: ExpoView { DispatchQueue.main.async { [weak self] in guard let self = self else { return } - let mediaOptions = source["mediaOptions"] as? [String: Any] + let mediaOptions = source["mediaOptions"] as? [String: Any] ?? [:] let initOptions = source["initOptions"] as? [Any] ?? [] let uri = source["uri"] as? String let autoplay = source["autoplay"] as? Bool ?? false @@ -133,18 +141,8 @@ class VlcPlayerView: ExpoView { } } - // Apply subtitle options - let subtitleOptions = self.getSubtitleOptions() - media.addOptions(subtitleOptions) - print("Debug: Applied subtitle options: \(subtitleOptions)") - - // // Apply any additional media options - // if let mediaOptions = mediaOptions { - // media.addOptions(mediaOptions) - // print("Debug: Applied additional media options: \(mediaOptions)") - // } else { - // print("Debug: No additional media options provided") - // } + print("Debug: Media options: \(mediaOptions)") + media.addOptions(mediaOptions) // Apply subtitle options let subtitleTrackIndex = source["subtitleTrackIndex"] as? Int ?? -1 @@ -610,9 +608,12 @@ extension VlcPlayerView: VLCMediaPlayerDelegate { guard let player = self.mediaPlayer else { return } let currentTimeMs = player.time.intValue + let remainingTimeMs = player.remainingTime let durationMs = player.media?.length.intValue ?? 0 + // print("currentTimeMs: \(currentTimeMs) RemainingTimeMs: \(remainingTimeMs)") if currentTimeMs >= 0 && currentTimeMs < durationMs { + self.onVideoProgress?([ "currentTime": currentTimeMs, "duration": durationMs,