<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Siddhesh Haryan</title>
    <description>The latest articles on DEV Community by Siddhesh Haryan (@siddhesh_haryan).</description>
    <link>https://dev.to/siddhesh_haryan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4260826%2Fc70762dd-36f7-4d18-b620-f4505863cf3f.png</url>
      <title>DEV Community: Siddhesh Haryan</title>
      <link>https://dev.to/siddhesh_haryan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/siddhesh_haryan"/>
    <language>en</language>
    <item>
      <title>Offline video downloads in Flutter: what I built for Kahani Play</title>
      <dc:creator>Siddhesh Haryan</dc:creator>
      <pubDate>Thu, 26 Nov 2026 10:40:51 +0000</pubDate>
      <link>https://dev.to/siddhesh_haryan/offline-video-downloads-in-flutter-what-i-built-for-kahani-play-4510</link>
      <guid>https://dev.to/siddhesh_haryan/offline-video-downloads-in-flutter-what-i-built-for-kahani-play-4510</guid>
      <description>&lt;p&gt;&lt;em&gt;Streaming is the easy half of a video app. Downloads that survive a killed app, a full phone and a flaky train connection are where most of the engineering goes.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I built the offline downloads in &lt;a href="https://siddhesh.studio/work/kahani-play" rel="noopener noreferrer"&gt;Kahani Play&lt;/a&gt;, an Indian streaming app for movies, web series and short clips that has passed 500,000 downloads. The app is Flutter on Android and iPhone. Downloads are encrypted, kept in app-private storage, and run through a small native Android layer so they keep going in the background. This guide is the architecture I'd use again, plus the parts I'd do differently.&lt;/p&gt;

&lt;p&gt;It's not a copy-paste tutorial. Package APIs change every few months. The decisions below don't.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why offline matters so much in India
&lt;/h2&gt;

&lt;p&gt;For a lot of Indian viewers, mobile data is metered and patchy. People download an episode on home or office Wi-Fi and watch it on the commute, on a train through dead zones, or at a relative's place with one bar of signal. For them a download button is not a nice extra. It decides whether they watch at all.&lt;/p&gt;

&lt;p&gt;It also changes how you build. Most of your users are on budget Android phones with limited storage and aggressive battery savers that kill background work. Design for that phone first and the flagship takes care of itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  HLS downloads vs plain MP4 files
&lt;/h2&gt;

&lt;p&gt;The first real choice is what you download. If you already stream HLS, which most OTT apps do, you can download the HLS stream itself or serve a separate MP4 file for offline.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Factor&lt;/th&gt;
&lt;th&gt;Download the HLS stream&lt;/th&gt;
&lt;th&gt;Separate MP4 per quality&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;What you store&lt;/td&gt;
&lt;td&gt;A playlist plus hundreds of small segment files&lt;/td&gt;
&lt;td&gt;One file per episode&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Resume after failure&lt;/td&gt;
&lt;td&gt;Natural: skip segments you already have&lt;/td&gt;
&lt;td&gt;Needs HTTP range requests and careful handling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Encryption&lt;/td&gt;
&lt;td&gt;Can reuse AES-128 segment keys from your stream&lt;/td&gt;
&lt;td&gt;You encrypt the file yourself&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server work&lt;/td&gt;
&lt;td&gt;None extra if you already package HLS&lt;/td&gt;
&lt;td&gt;Extra encoding and storage for every title&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Playback offline&lt;/td&gt;
&lt;td&gt;Rewrite the playlist to point at local files&lt;/td&gt;
&lt;td&gt;Point the player at one local file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Progress reporting&lt;/td&gt;
&lt;td&gt;Count segments, easy and accurate&lt;/td&gt;
&lt;td&gt;Bytes received, also easy&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;How the two options compare for offline viewing&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I lean towards downloading HLS. You already have the renditions, resuming is just picking up the next missing segment, and you don't double your encoding and storage bill. The cost is complexity on the client: you parse the master playlist, choose a rendition, download every segment and the key, then write a local playlist the player can open. MP4 is simpler to build and fine for a small catalogue, so don't dismiss it if you're shipping an MVP.&lt;/p&gt;

&lt;h2&gt;
  
  
  Picking the quality to download
&lt;/h2&gt;

&lt;p&gt;Don't download the top rendition by default. A 1080p episode can eat a gigabyte, and on a phone with 64 GB and a full WhatsApp folder, that's a failed download and an angry review.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Offer two or three named choices, like Data Saver, Standard and High, mapped to renditions in your master playlist.&lt;/li&gt;
&lt;li&gt;Show the estimated size next to each choice. You can work it out from the rendition bandwidth and the episode duration.&lt;/li&gt;
&lt;li&gt;Remember the user's last pick and let them set a default in settings.&lt;/li&gt;
&lt;li&gt;Check free storage before starting, with headroom, and say so plainly if there isn't enough.&lt;/li&gt;
&lt;li&gt;Keep audio in mind if you use separate audio renditions. You need to download the audio playlist too, or the file plays silent.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Background downloads on Android and iOS
&lt;/h2&gt;

&lt;p&gt;This is the part that pushed Kahani Play into native code. A download running inside Dart dies when the user swipes the app away or the OS reclaims memory. Episodes take minutes, sometimes longer, so the work has to live outside the Flutter UI.&lt;/p&gt;

&lt;h3&gt;
  
  
  Android
&lt;/h3&gt;

&lt;p&gt;On Android I wrote a small Kotlin layer that owns the download queue and talks to Flutter over a platform channel. Long-running user-initiated transfers belong in WorkManager with a foreground notification, or a foreground service of the data sync type, depending on your target SDK. Recent Android versions require you to declare the foreground service type and are stricter each year, so check the current rules for your targetSdkVersion. Show a notification with progress and a cancel action. Users expect it, and it keeps the system from killing you.&lt;/p&gt;

&lt;h3&gt;
  
  
  iOS
&lt;/h3&gt;

&lt;p&gt;On iOS the tool is a background URLSession. You hand transfers to the system and it carries on while the app is suspended, then wakes the app when they finish. For HLS specifically, Apple has its own AVAssetDownloadURLSession that downloads an HLS asset for offline playback, and it's the route you'd take if you ever add FairPlay. iOS gives you less control over timing than Android, so design your UI to tolerate downloads that finish later than expected.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flutter packages as a starting point
&lt;/h3&gt;

&lt;p&gt;If you'd rather not write native code on day one, background_downloader and flutter_downloader both wrap the platform background APIs for file downloads. They're worth evaluating for MP4 or for a first version. For HLS with many segments and encryption, I ended up wanting full control of the queue, which is why Kahani Play has its own native layer. Treat any package as an option to test on real devices, not a decision you can make from the README.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Keep the download engine native and the download UI in Flutter. The engine needs to survive the app being killed. The screens just need to read its state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Pause, resume and retry
&lt;/h2&gt;

&lt;p&gt;Every download will be interrupted. The network drops, the user walks out of Wi-Fi, the phone restarts. What I'd build every time:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Persist the queue and each item's progress in a local database, not in memory. On restart, rebuild state from disk.&lt;/li&gt;
&lt;li&gt;For HLS, record which segments are complete. Resume means fetch the next missing one.&lt;/li&gt;
&lt;li&gt;Retry failed segments with exponential backoff and a cap, then mark the item as failed with a clear reason.&lt;/li&gt;
&lt;li&gt;Respect a Wi-Fi only setting and pause on mobile data when it's on. Many Indian users want this as the default.&lt;/li&gt;
&lt;li&gt;Refresh signed URLs before resuming. CDN links often expire, and a resume against a dead URL looks like a network error.&lt;/li&gt;
&lt;li&gt;Limit concurrent downloads, two at most on a budget phone, so the device stays usable.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Storage, encryption and DRM
&lt;/h2&gt;

&lt;p&gt;Content owners care about one thing here: a downloaded episode should not be shareable as a file. There's a ladder of options, and you pick a rung based on your content deals and budget.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;What it stops&lt;/th&gt;
&lt;th&gt;Cost and effort&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;App-private storage&lt;/td&gt;
&lt;td&gt;Casual copying from the file manager or gallery&lt;/td&gt;
&lt;td&gt;Low. Use the app's internal directory, never shared storage.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Encrypted files with a key from your server&lt;/td&gt;
&lt;td&gt;Pulling files off a rooted phone and playing them elsewhere&lt;/td&gt;
&lt;td&gt;Medium. Key handling and local decryption for playback.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Widevine / FairPlay offline licences&lt;/td&gt;
&lt;td&gt;Most extraction, with licence rules enforced by the platform&lt;/td&gt;
&lt;td&gt;High. A DRM provider, licence server, packaging and more testing.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Protection options for downloaded video, from lightest to heaviest&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Kahani Play uses the first two: files live in app-private storage and they're encrypted. That was the right level for its catalogue. If a studio contract requires hardware-backed DRM, you move to Widevine offline licences on Android and FairPlay persistent keys on iOS, usually through a DRM vendor. It's a real project on its own, and it narrows your player choices, so find out early whether your contracts need it.&lt;/p&gt;

&lt;p&gt;Wherever the key lives, don't hardcode it in the app. Fetch it per title from your backend after an entitlement check and keep it in the Android Keystore or iOS Keychain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Expiry and licence checks
&lt;/h2&gt;

&lt;p&gt;A download is a promise with conditions. Typical rules are: playable for a set number of days, a shorter window once playback starts, and only while the subscription is active. Store an expiry with each download and check it before playback.&lt;/p&gt;

&lt;p&gt;The harder question is how long to trust the device offline. I'd require the app to check in with the server every few days to confirm the subscription, and lock downloads, not delete them, if it can't. Deleting a 700 MB file because someone was on a two-week trip is a bad experience. If downloads are tied to a paid plan, that check depends on your billing being reliable, which is what my &lt;a href="https://siddhesh.studio/guides/razorpay-subscriptions-flutter" rel="noopener noreferrer"&gt;Razorpay subscriptions in Flutter&lt;/a&gt; guide covers.&lt;/p&gt;

&lt;h2&gt;
  
  
  UI states you'll need
&lt;/h2&gt;

&lt;p&gt;The download button looks like one widget. In practice it has at least these states, and each needs a design:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Not downloaded, with the size estimate.&lt;/li&gt;
&lt;li&gt;Queued, waiting for Wi-Fi or another download.&lt;/li&gt;
&lt;li&gt;Downloading, with progress and a pause action.&lt;/li&gt;
&lt;li&gt;Paused, by the user or by the system.&lt;/li&gt;
&lt;li&gt;Failed, with a reason in plain words and a retry button.&lt;/li&gt;
&lt;li&gt;Downloaded and playable.&lt;/li&gt;
&lt;li&gt;Expired or locked, with a way to renew.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Add a Downloads screen that works with no network at all. People open it in airplane mode, so it must not wait on an API call. For playback, video_player can play local files, and media_kit and better_player are other options worth comparing for HLS features and subtitles. Test the one you pick with your actual encrypted local playlists before committing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing on cheap phones and bad networks
&lt;/h2&gt;

&lt;p&gt;Emulators on a fast office connection will tell you almost nothing. My test list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A budget Android phone from a brand with aggressive battery management. Start a download, lock the phone, leave it for 20 minutes.&lt;/li&gt;
&lt;li&gt;Swipe the app away mid-download. Reboot mid-download.&lt;/li&gt;
&lt;li&gt;Toggle airplane mode repeatedly. Switch Wi-Fi to mobile data with Wi-Fi only on.&lt;/li&gt;
&lt;li&gt;Fill the storage until it's nearly full, then download.&lt;/li&gt;
&lt;li&gt;Throttle the network to a slow 3G profile and watch retries.&lt;/li&gt;
&lt;li&gt;Let a signed URL expire during a pause, then resume.&lt;/li&gt;
&lt;li&gt;Change the device clock to test expiry, then make sure the server check catches it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Mistakes I see most often
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Running downloads in Dart isolates and assuming they'll survive the app being closed.&lt;/li&gt;
&lt;li&gt;Saving video to shared storage, where any file manager can copy it.&lt;/li&gt;
&lt;li&gt;Shipping the decryption key inside the app.&lt;/li&gt;
&lt;li&gt;Defaulting to the highest quality and never checking free space.&lt;/li&gt;
&lt;li&gt;Keeping download state only in memory, so a restart shows everything as not downloaded while gigabytes sit on disk.&lt;/li&gt;
&lt;li&gt;Forgetting cleanup. When a user logs out or a licence expires, files should go, or at least become unplayable.&lt;/li&gt;
&lt;li&gt;Testing only on a flagship phone.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're planning a streaming app and want to know what this adds to the budget, I've broken it down in &lt;a href="https://siddhesh.studio/guides/ott-app-development-cost-india" rel="noopener noreferrer"&gt;OTT app development cost in India&lt;/a&gt;. If you're still deciding on a stack, &lt;a href="https://siddhesh.studio/guides/flutter-vs-react-native" rel="noopener noreferrer"&gt;Flutter vs React Native&lt;/a&gt; explains why I was happy doing this in Flutter with a native layer.&lt;/p&gt;

&lt;p&gt;I build this kind of thing as part of &lt;a href="https://siddhesh.studio/services/flutter-app-development" rel="noopener noreferrer"&gt;Flutter app development&lt;/a&gt;, and the licence and entitlement side as &lt;a href="https://siddhesh.studio/services/backend-development" rel="noopener noreferrer"&gt;backend development&lt;/a&gt;. If you already have an app whose downloads keep failing, &lt;a href="https://siddhesh.studio/services/app-maintenance" rel="noopener noreferrer"&gt;app maintenance&lt;/a&gt; is where I'd start. If you'd rather staff it in-house, read &lt;a href="https://siddhesh.studio/guides/hire-flutter-developer" rel="noopener noreferrer"&gt;how to hire a Flutter developer&lt;/a&gt; first. Either way, tell me about it on the &lt;a href="https://siddhesh.studio/contact" rel="noopener noreferrer"&gt;contact page&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Can Flutter download HLS videos for offline playback?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yes. You parse the HLS master playlist, pick a rendition, download its segments and key, and write a local playlist the player can open. The download itself is best handled by native background APIs, which you can reach through a package or your own platform channel. Kahani Play does this with a native Android layer and a Flutter UI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Which Flutter package is best for background video downloads?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;background_downloader and flutter_downloader are the common options for file downloads, and both use the platform's background APIs. They work well for MP4 files. For HLS with many segments, encryption and custom retry rules, I prefer a small native layer per platform so I control the queue fully. Test any option on budget Android phones before deciding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do I stop users from sharing downloaded videos?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Store files in app-private storage, encrypt them, and fetch the key from your server after checking the user's subscription. That stops casual copying and most file sharing. If your content contracts require more, use Widevine offline licences on Android and FairPlay on iOS, which is a bigger project usually done through a DRM provider.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do I need DRM for offline downloads?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Not always. Many apps ship with app-private storage and encrypted files, which is what Kahani Play uses. Studio or broadcaster content often comes with a contract that requires Widevine and FairPlay. Check your content agreements before you build, because DRM changes your player choice, your backend and your testing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why do my Flutter downloads stop when the app is closed?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because they're running in the Dart side of the app, which stops when the app is killed or suspended. Move the download engine to WorkManager or a foreground service on Android and a background URLSession on iOS, and keep the queue state on disk so the app can rebuild it on restart.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How long should offline downloads stay playable?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's a business rule, often set by content licences. A common pattern is a fixed window after download, a shorter window after first play, and a requirement that the app confirms the subscription with the server every few days. If it can't, lock the downloads rather than deleting them.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This was first published on my site: &lt;a href="https://siddhesh.studio/guides/flutter-offline-video-downloads" rel="noopener noreferrer"&gt;Offline video downloads in Flutter: what I built for Kahani Play&lt;/a&gt;. I'm Siddhesh, an independent app developer in Mumbai. I build Flutter and React Native apps, including &lt;a href="https://siddhesh.studio/work/kahani-play" rel="noopener noreferrer"&gt;Kahani Play&lt;/a&gt;, a streaming app with 500,000+ downloads.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>mobile</category>
      <category>android</category>
    </item>
  </channel>
</rss>
