From dfb86e35f154d1f29abc41d4e2049c36d2c52e06 Mon Sep 17 00:00:00 2001 From: playbridgeapp Date: Sun, 29 Nov 2026 00:08:53 -0330 Subject: [PATCH] fix(tv/apple): stop the AVPlayer seek bar bouncing after a seek After AVPlayer.seek the player reports the old position and then where it landed, which on HLS is often a few seconds before the target, so the bar went forward, back and forward again. The bar now stays on the seek target until playback reaches it, the player is paused after landing, or 6 seconds pass. Positions sent to phones and the party are unchanged. Co-Authored-By: Claude Sonnet 5.5 --- .../Player/NativePlayerView.swift | 22 ++++++++++- .../PlayBridge TV/Player/SeekBarHold.swift | 39 +++++++++++++++++++ tv/apple/tests/ReceiverReviewTests.swift | 17 ++++++++ tv/apple/tests/run-receiver-review-checks.sh | 1 + 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 tv/apple/PlayBridge TV/PlayBridge TV/Player/SeekBarHold.swift diff --git a/tv/apple/PlayBridge TV/PlayBridge TV/Player/NativePlayerView.swift b/tv/apple/PlayBridge TV/PlayBridge TV/Player/NativePlayerView.swift index 306ccdcd..73169dd8 100644 --- a/tv/apple/PlayBridge TV/PlayBridge TV/Player/NativePlayerView.swift +++ b/tv/apple/PlayBridge TV/PlayBridge TV/Player/NativePlayerView.swift @@ -416,6 +416,8 @@ struct NativePlayerView: UIViewControllerRepresentable { if let initialTime { playbackState.currentTime = initialTime } + seekHold = SeekBarHold() + seekHoldToken += 1 let wasPreBuffering = controlsSuppressedByPreplay controlsSuppressedByPreplay = isPreBuffering playbackState.prebuffering = isPreBuffering @@ -483,6 +485,12 @@ struct NativePlayerView: UIViewControllerRepresentable { playbackState.hasSidecarSubtitleOverlay = active } + /// After a seek, AVPlayer reports the old position until the seek finishes and then where it + /// really landed, which on HLS can be a few seconds before the target. Showing those makes + /// the seek bar go forward, back and forward again, so the bar stays on the target until + /// playback gets there. The reported position (phones, watch party) is always the real one. + private var seekHold = SeekBarHold() + private var seekHoldToken = 0 private var timeObserver: Any? private var stallTimer: Timer? private var stallDetector = PlaybackStallDetector() @@ -932,7 +940,8 @@ struct NativePlayerView: UIViewControllerRepresentable { let durRaw = player.currentItem?.duration.seconds ?? 0 let dur = (itemLoaded && durRaw.isFinite && durRaw > 0) ? durRaw : 0 let bufferedEndSeconds = bufferedEnd(player: player, position: pos) - playbackState.currentTime = pos + playbackState.currentTime = seekHold.displayedPosition( + actual: pos, paused: player.timeControlStatus == .paused, now: Date()) playbackState.duration = dur if let buffered = bufferedEndSeconds, buffered.isFinite { playbackState.bufferedTime = max(pos, buffered) @@ -1440,7 +1449,16 @@ struct NativePlayerView: UIViewControllerRepresentable { private func seek(to seconds: Double) { stallDetector.reset() guard seconds.isFinite, seconds >= 0 else { return } - player?.seek(to: CMTime(seconds: seconds, preferredTimescale: 600)) + seekHold.begin(target: seconds, now: Date()) + seekHoldToken += 1 + let token = seekHoldToken + player?.seek(to: CMTime(seconds: seconds, preferredTimescale: 600)) { [weak self] _ in + DispatchQueue.main.async { + guard let self, token == self.seekHoldToken else { return } + self.seekHold.finishSeek() + self.broadcastStatus() + } + } } private func selectTrack(_ characteristic: AVMediaCharacteristic, id: String) { diff --git a/tv/apple/PlayBridge TV/PlayBridge TV/Player/SeekBarHold.swift b/tv/apple/PlayBridge TV/PlayBridge TV/Player/SeekBarHold.swift new file mode 100644 index 00000000..df527d7e --- /dev/null +++ b/tv/apple/PlayBridge TV/PlayBridge TV/Player/SeekBarHold.swift @@ -0,0 +1,39 @@ +import Foundation + +/// Keeps the seek bar on a seek's target while the player catches up. +/// +/// After `AVPlayer.seek`, the player first reports the old position, then the spot it landed on +/// (HLS lands on a segment start, often seconds before the target). Showing both moves the bar +/// forward, back and forward again. The bar stays on the target until playback reaches it, the +/// player is paused after landing, or a time limit passes. +struct SeekBarHold { + /// Longest the bar stays on a target without the player reaching it. + static let maxHoldSeconds: TimeInterval = 6 + /// How close the player has to be to the target to count as arrived. + static let arrivedWithinSeconds: Double = 1 + + private var target: Double? + private var deadline: Date = .distantPast + private var seekFinished = false + + mutating func begin(target: Double, now: Date) { + self.target = target + deadline = now.addingTimeInterval(Self.maxHoldSeconds) + seekFinished = false + } + + mutating func finishSeek() { + seekFinished = true + } + + /// The position to show for the player's real position `actual`. + mutating func displayedPosition(actual: Double, paused: Bool, now: Date) -> Double { + guard let target else { return actual } + let arrived = abs(actual - target) <= Self.arrivedWithinSeconds + if arrived || now >= deadline || (seekFinished && paused) { + self.target = nil + return actual + } + return target + } +} diff --git a/tv/apple/tests/ReceiverReviewTests.swift b/tv/apple/tests/ReceiverReviewTests.swift index 367a209f..6c608418 100644 --- a/tv/apple/tests/ReceiverReviewTests.swift +++ b/tv/apple/tests/ReceiverReviewTests.swift @@ -3450,6 +3450,23 @@ private func runDevice6FocusAndHomeTests() { assert(TogetherRejoin.startPositionMs(timelinePositionMs: 60_000, playing: true, elapsedMs: 5_000) == 65_000) assert(TogetherRejoin.startPositionMs(timelinePositionMs: 60_000, playing: false, elapsedMs: 5_000) == 60_000) assert(TogetherRejoin.startPositionMs(timelinePositionMs: -5, playing: true, elapsedMs: -9) == 0) + // Seek bar: stays on the seek target while the player reports the old and the landed position. + var hold = SeekBarHold() + let t0 = Date(timeIntervalSince1970: 1_000) + assert(hold.displayedPosition(actual: 10, paused: false, now: t0) == 10) + hold.begin(target: 120, now: t0) + assert(hold.displayedPosition(actual: 10, paused: false, now: t0 + 1) == 120) + hold.finishSeek() + assert(hold.displayedPosition(actual: 117, paused: false, now: t0 + 2) == 120) + assert(hold.displayedPosition(actual: 119.5, paused: false, now: t0 + 3) == 119.5) + assert(hold.displayedPosition(actual: 119.6, paused: false, now: t0 + 4) == 119.6) + hold.begin(target: 50, now: t0) + assert(hold.displayedPosition(actual: 30, paused: false, now: t0 + 7) == 30) + hold.begin(target: 50, now: t0) + hold.finishSeek() + assert(hold.displayedPosition(actual: 47, paused: true, now: t0 + 1) == 47) + hold.begin(target: 50, now: t0) + assert(hold.displayedPosition(actual: 47, paused: true, now: t0 + 1) == 50) assert(TogetherStepFocusTarget.target(isAvailable: true, stepKey: "ended") == .endedOk) assert(TogetherStepFocusTarget.target(isAvailable: false, stepKey: "idle") == .unavailableBack) assert(TogetherStepFocusTarget.target(isAvailable: false, stepKey: "invited") == .unavailableBack) diff --git a/tv/apple/tests/run-receiver-review-checks.sh b/tv/apple/tests/run-receiver-review-checks.sh index 9970ec0b..d65ed87d 100644 --- a/tv/apple/tests/run-receiver-review-checks.sh +++ b/tv/apple/tests/run-receiver-review-checks.sh @@ -20,6 +20,7 @@ swiftc -module-cache-path "$test_dir/cache" \ "$source_root/UI/SubtitleFontLibrary.swift" \ "$source_root/UI/SubtitleStyleSettings.swift" \ "$source_root/Together/TogetherModels.swift" \ + "$source_root/Player/SeekBarHold.swift" \ "$source_root/Together/TogetherTvPartyState.swift" \ "$source_root/Together/TogetherStepFocusTarget.swift" \ "$source_root/Together/TogetherClient.swift" \