Resumable playback for Flutter
Resumable playback for Flutter
FastPix Flutter SDK lets you resume video playback from the last watched position by saving progress and passing it back as the start position.
1. How it works
The file imports dart:async for the progress timer, Flutter's widgets library for the app lifecycle observer, and the FastPix Video Player package, which provides FastPixPlayerController, FastPixPlayerDataSource, the FastPixPlayer widget, and the player event types.
2. Setting up the player state
3. Resuming from the saved position
4. Saving progress on pause and after seeks
5. Clearing progress on completion
6. Tracking progress during playback
7. The save helper
8. Saving progress when the app goes to the background
9. Cleaning up resources
10. Connecting your storage
1import 'dart:async';
2import 'package:flutter/widgets.dart';
3import 'package:fastpix_video_player/fastpix_video_player.dart';
4
5class ResumablePlayerPage extends StatefulWidget {
6 const ResumablePlayerPage({super.key, required this.playbackId});
7
8 final String playbackId;
9
10 @override
11 State<ResumablePlayerPage> createState() => _ResumablePlayerPageState();
12}
13
14class _ResumablePlayerPageState extends State<ResumablePlayerPage>
15 with WidgetsBindingObserver {
16 final controller = FastPixPlayerController();
17 Timer? _progressTimer;
18 bool _completed = false;
19
20 @override
21 void initState() {
22 super.initState();
23 WidgetsBinding.instance.addObserver(this);
24 _startPlayback();
25 }
26
27 // 1. Resume from the saved position
28 Future<void> _startPlayback() async {
29 final savedPosition = await loadProgress(widget.playbackId);
30 if (!mounted) return;
31
32 await controller.initialize(
33 dataSource: FastPixPlayerDataSource.hls(
34 playbackId: widget.playbackId,
35 startAt: savedPosition, // null starts from the beginning
36 ),
37 );
38 if (!mounted) return;
39
40 // 2. Save progress on pause and after seeks
41 controller.addEventListener(FastPixPlayerEventTypes.pause, (_) =>
42 _saveProgress());
43 controller.addEventListener(FastPixPlayerEventTypes.seeked, (_) =>
44 _saveProgress());
45
46 // 3. Clear progress when playback completes
47 controller.addEventListener(FastPixPlayerEventTypes.finished, (_) {
48 _completed = true;
49 clearProgress(widget.playbackId);
50 });
51 controller.addEventListener(FastPixPlayerEventTypes.play, (_) =>
52 _completed = false);
53
54 // 4. Save progress periodically while playing
55 _progressTimer = Timer.periodic(const Duration(seconds: 5), (_) {
56 if (controller.isPlaying) _saveProgress();
57 });
58 }
59
60 void _saveProgress() {
61 if (_completed) return;
62 final position = controller.getCurrentPosition();
63 final duration = controller.getTotalDuration();
64 if (position == null || position == Duration.zero) return;
65 saveProgress(widget.playbackId, position, duration);
66 }
67
68 // 5. Save when the app goes to the background
69 @override
70 void didChangeAppLifecycleState(AppLifecycleState state) {
71 if (state == AppLifecycleState.paused) _saveProgress();
72 }
73
74 // 6. Save once more before releasing the player
75 @override
76 void dispose() {
77 _saveProgress();
78 _progressTimer?.cancel();
79 WidgetsBinding.instance.removeObserver(this);
80 controller.dispose();
81 super.dispose();
82 }
83
84 @override
85 Widget build(BuildContext context) => FastPixPlayer(controller: controller);
86}
87
88// Retrieve the last saved position from your app / backend.
89Future<Duration?> loadProgress(String playbackId) async {
90 return null;
91}
92
93// Save progress in your app / backend.
94void saveProgress(String playbackId, Duration position, Duration? duration) {}
95
96// Clear saved progress in your app / backend.
97void clearProgress(String playbackId) {}