media_cache_manager
This package helps you to cache media (Audio, Video, Image, etc...).
AhmedAbogameel/media_cache_manager open-source repository details.
^2.1.5^5.9.0^2.1.1^3.1.1{"sdk":"flutter"}^2.0.0English project snapshot. Visit GitHub for the latest content.
⚡ Helps you to cache and encrypt media (Audio 🎵, Video 🎬, Image 🖼️, etc...) Permanently or for a specific time.
With just a URL, the DownloadMediaBuilder widget will:
daysToExpire behavior improved.autoDownload option to enable/disable auto download.onInit ➝ onInitialize.DownloadMediaStatus.DownloadMediaBuilder widget.Add to your pubspec.yaml:
dependencies:
media_cache_manager:
defaultConfig:multiDexEnabled true
minSdkVersion 20
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await MediaCacheManager.instance.init();
runApp(const MyApp());
}
Encrypt media files using AES (OFB mode).
await MediaCacheManager.instance.setEncryptionPassword("I love flutter");
or directly on init:
await MediaCacheManager.instance.init(encryptionPassword: 'I love flutter');
⚠️ Notes:
DownloadMediaBuilder(
url: "https://example.com/sample.jpg",
encryptionPassword: "this is a password",
onSuccess: (snapshot) {
return Image.file(File(snapshot.filePath!));
}
)
Make cached files expire automatically after specific days.
await MediaCacheManager.instance.setExpireDate(daysToExpire: 10);
or via init:
await MediaCacheManager.instance.init(daysToExpire: 1);
📌 Behavior: If a file is accessed before its expiration → expiration will extend automatically.
DownloadMediaBuilder(
url: 'https://example.com/sample.jpg',
onSuccess: (snapshot) {
return Image.file(File(snapshot.filePath!));
},
onLoading: (snapshot) {
return LinearProgressIndicator(value: snapshot.progress);
},
),
By default, autoDownload = true.
You can disable it and control downloads manually:
late DownloadMediaBuilderController controller;
DownloadMediaBuilder(
url: 'https://example.com/video.mp4',
autoDownload: false,
onInitialize: (ctrl) => controller = ctrl,
onInitial: (_) {
return ElevatedButton(
onPressed: controller.getFile,
child: const Text('Load file'),
);
},
onSuccess: (snapshot) => BetterPlayer.file(snapshot.filePath!),
onLoading: (snapshot) => LinearProgressIndicator(value: snapshot.progress),
),
DownloadMediaBuilder(
url: 'https://example.com/video.mp4',
onLoading: (snapshot) => LinearProgressIndicator(value: snapshot.progress),
onSuccess: (snapshot) => BetterPlayer.file(snapshot.filePath!),
onError: (_) => const Text('❌ Error!'),
),
late DownloadMediaBuilderController controller;
DownloadMediaBuilder(
url: 'https://example.com/video.mp4',
onInitialize: (ctrl) => controller = ctrl,
onLoading: (snapshot) {
return Column(
children: [
LinearProgressIndicator(value: snapshot.progress),
ElevatedButton(
onPressed: controller.cancel,
child: const Text('Cancel Download'),
),
],
);
},
onSuccess: (snapshot) => BetterPlayer.file(snapshot.filePath!),
onError: (_) => const Text('❌ Error!'),
onCancel: (_) {
return ElevatedButton(
onPressed: controller.retry,
child: const Text('Retry'),
);
},
),
📌 Notes:
loading.canceled.DownloadMediaSnapshot contains 3 fields:
Initial, Success, Loading, Error, Canceled, Encrypting, Decrypting.