Flutter Video Player: A Practical Example and Tuning Guide
Video playback looks simple until you ship it: rotation loses position, buffering stalls on poor networks, and the player keeps decoding in the background draining battery. This guide walks through a complete Flutter video player example using the video_player package, then covers the tuning that separates a demo from a production feature.
You will get a working screen with play/pause, seek, and fullscreen handling, plus practical numbers for buffer targets, controller disposal, and list performance so your player behaves well on mid-range Android devices and iPhones alike.
Set up the video_player package correctly
Add video_player to pubspec.yaml and run pub get. On Android, set minSdkVersion to at least 21; on iOS, set the platform to 12.0 or higher. If you load remote URLs, declare INTERNET in the Android manifest and allow arbitrary loads in Info.plist for development only. Keep the dependency version current because playback fixes land frequently.
Create a VideoPlayerController in initState, not build. Choose the constructor that matches your source: asset, networkUrl, or file. Call initialize() and then setLooping or setVolume as needed before setState triggers the first frame. Wrap the whole flow in a try/catch because a bad URL or unsupported codec throws asynchronously and will otherwise crash the screen.
- Use VideoPlayerController.asset for bundled files under assets/.
- Use VideoPlayerController.networkUrl for HLS or MP4 streams.
- Always await initialize() before reading value.duration.
- Guard against calling setState after dispose with a mounted check.
Wire the controller lifecycle to the widget lifecycle
The controller holds a platform texture and native decoder; leaking it is expensive. Call dispose in your State's dispose method and pause playback in didChangeAppLifecycleState when the app goes to background. A common bug is a video that keeps playing audio after the user navigates away because the route was popped without disposal.
For a screen that can rotate, do not recreate the controller on orientation change. Instead store the controller in the State and rebuild only the widget tree around it. Keep the current position in the controller itself, and rebuild using ValueListenableBuilder on controller so you avoid calling setState on every position tick, which can fire several times per second.
Build simple, reliable playback controls
A minimal control bar needs play/pause, a seek slider, elapsed and total time, and a fullscreen toggle. Drive the slider from controller.value.position and its max from controller.value.duration. When the user drags, pause updates to the slider and seek on drag end with seekTo, otherwise the thumb fights the playback position.
Show a buffering indicator when controller.value.isBuffering is true. For fullscreen, push a new route that reuses the same controller rather than creating a second one; a second controller on the same URL doubles memory and can desync audio. If you use a scrolling layout around the player, review Scrolling Widgets Compared: ListView, GridView and CustomScrollView so the player does not rebuild during scroll.
- Seek on drag end, not on every drag update.
- Format durations as mm:ss and handle nulls before duration loads.
- Reuse one controller for fullscreen routes.
- Disable controls while isBuffering to avoid double taps.
Handle buffering and network behaviour
For remote MP4s, the platform player buffers ahead on its own; you mostly control this through the source. Prefer HLS for longer videos because it adapts bitrate to bandwidth. Expect initial startup of roughly 1 to 3 seconds on a decent connection and noticeably longer on 3G-class networks; show a spinner after 300-500 ms so fast loads do not flash.
If you need preloading, initialize the controller early and hold it, but be aware each active controller consumes memory. On low-end Android devices, two 1080p decoders can push the app close to its memory limit. Keep bitrates moderate (around 2-5 Mbps for 1080p) and test on a real mid-range phone rather than only an emulator.
| Source | Startup time | Seek latency | Memory per player | Best for |
|---|---|---|---|---|
| Bundled asset (720p) | Under 0.5 s | Under 100 ms | 40-80 MB | Short onboarding clips |
| Remote MP4 (1080p, 5 Mbps) | 1-3 s | 200-600 ms | 60-120 MB | Short standalone videos |
| HLS adaptive | 1-3 s | 200-800 ms | 60-120 MB | Long or variable-network video |
| Remote MP4 on 3G-class | 4-8 s | 1-2 s | 60-120 MB | Avoid; use HLS |
Keep playback smooth inside lists
Video inside a scrolling feed is where performance dies. Only one player should be active at a time. Track visibility with a package such as visibility_detector and pause controllers that scroll off screen; dispose them when they leave the cache extent. Avoid building a controller per item eagerly, because a list of ten videos can create ten decoders.
The same sizing discipline that keeps lists fast applies here: item extents, cache extent, and rebuild scope. The techniques in How to Size a RecyclerView Adapter for Smooth Lists translate directly to Flutter list tuning. Keep the player widget in its own StatefulWidget so position ticks rebuild only the control bar, not the whole feed item.
Test across devices and edge cases
Test at least one low-end Android phone, one mid-range phone, and one iPhone. Check rotation, backgrounding, headphone unplug, incoming calls, and network loss mid-playback. Verify that audio pauses when another app takes audio focus on Android; the platform usually handles this, but custom audio sessions can break it.
Finally, measure. Use Flutter DevTools to watch frame times during playback; jank above 16 ms per frame shows up as stutter. Profile memory before and after opening the player, and confirm the controller is disposed when you leave the screen. If video is one part of a larger product, budgeting time for this polish matters; see What Does a Flutter App Cost to Build in 2026? for how media features affect scope.
Key points
- Create and dispose the VideoPlayerController in State methods, never in build.
- Seek on drag end and rebuild controls with ValueListenableBuilder to avoid jank.
- Allow one active player at a time in lists and pause off-screen items.
- Test on a real low-end Android device; emulator playback hides most problems.
Flutter Screen Widget and Cost Estimator
Estimate the number of widgets in a screen and a rough development-hour figure from your component counts. Adjust the fields to see how each UI area affects the total effort.
Editor and Flutter contributor
Writes practical Flutter tutorials focused on widgets, layout and animation. Enjoys breaking large UI problems into small, testable pieces and explaining them clearly to beginners.