Resumable playback for iOS

Resumable playback for iOS
FastPix iOS SDK lets you resume video playback from the last watched position by saving progress and restoring it when the player is ready.
1. How it works
The file imports SwiftUI and the FastPix Player SDK, providing access to the player view controller, seek delegates, and playlist notifications needed for resumable playback.
2. Setting up the Coordinator
3. Reading the saved position
4. Restoring position on first time update
5. Persisting position after seeks
6. Saving and clearing helpers
7. Persisting on background or dismissal
8. Resetting on playlist item change
1import SwiftUI
2import FastPixPlayerSDK
3
4// MARK: - 1. Save & restore position in the Coordinator
5
6final class Coordinator: FastPixSeekDelegate {
7
8 weak var hostVC: FastPixPlayerHostVC?
9 private var hasSeekedToSavedPosition = false
10
11 // Retrieve the last saved position from your app / backend.
12 private var savedPosition: TimeInterval {
13 return UserDefaults.standard.double(forKey: "lastPlaybackPosition")
14 }
15
16 // onTimeUpdate: called periodically — save latest progress.
17 func onTimeUpdate(currentTime: TimeInterval, duration: TimeInterval) {
18 hostVC?.seekBar.updateTime(current: currentTime, duration: duration)
19 saveProgress(currentTime)
20
21 // On first update after load, seek to the saved position.
22 if !hasSeekedToSavedPosition && savedPosition > 0 {
23 hasSeekedToSavedPosition = true
24 hostVC?.playerViewController.seek(to: savedPosition)
25 }
26 }
27
28 // onSeekEnd: also persist immediately after any seek.
29 func onSeekEnd(at time: TimeInterval) {
30 let dur = hostVC?.playerViewController.getDuration() ?? 0
31 hostVC?.seekBar.updateTime(current: time, duration: dur)
32 saveProgress(time)
33 }
34
35 // Save progress to your app / backend.
36 private func saveProgress(_ time: TimeInterval) {
37 UserDefaults.standard.set(time, forKey: "lastPlaybackPosition")
38 }
39
40 // Clear saved progress when playback completes.
41 func clearSavedProgress() {
42 UserDefaults.standard.removeObject(forKey: "lastPlaybackPosition")
43 hasSeekedToSavedPosition = false
44 }
45}
46
47// MARK: - 2. Persist on app background / disappear in FastPixPlayerHostVC
48
49override func viewWillDisappear(_ animated: Bool) {
50 super.viewWillDisappear(animated)
51
52 if playerViewController.isPiPActive() { return }
53
54 if isMovingFromParent || isBeingDismissed {
55 let currentTime = playerViewController.getCurrentTime()
56 coordinator?.saveProgress(currentTime) // persist before releasing
57 playerViewController.pause()
58 playerViewController.player = nil
59 }
60}
61
62// MARK: - 3. Reset saved progress on playlist item change
63
64@objc private func playlistStateChanged(_ notification: Notification) {
65 coordinator?.clearSavedProgress()
66 seekBar.resetBuffer()
67}