media_cache_manager 0.3.1
media_cache_manager: ^0.3.1 copied to clipboard
This package helps you to cache media (Audio, Video, Image, etc...).
π¦ media_cache_manager #
β‘ 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:
- π Search locally for the file.
- π If found β File will be returned in a snapshot.
- β¬οΈ If not found β File will be downloaded and stored locally, then returned in a snapshot.
β¨ Whatβs New? #
- ποΈ
daysToExpire
behavior improved. - βοΈ
autoDownload
option to enable/disable auto download. - π Renamed
onInit
βonInitialize
. - π Added initial state to
DownloadMediaStatus
. - π Encrypt & decrypt files with AES.
- βΉοΈ Cancel downloads anytime.
- π Retry failed downloads.
- π Added Encrypting / Decrypting states.
- π§© Refactored
DownloadMediaBuilder
widget. - π Optimized plugin imports.
π₯ Install #
Add to your pubspec.yaml
:
dependencies:
media_cache_manager:
βοΈ Android Setup #
- Open android β app β build.gradle
- Add this inside
defaultConfig
:
multiDexEnabled true
minSdkVersion 20
π Initializing Plugin (Required) #
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await MediaCacheManager.instance.init();
runApp(const MyApp());
}
π Encrypted Files (Optional) #
Encrypt media files using AES (OFB mode).
β Enable encryption globally #
await MediaCacheManager.instance.setEncryptionPassword("I love flutter");
or directly on init:
await MediaCacheManager.instance.init(encryptionPassword: 'I love flutter');
β οΈ Notes:
- π’ Large files may take more time to en/decrypt.
- π Files are stored in Temporary Directory, so users & other apps canβt access them.
- π Keep passwords safe! The plugin does not cache your file passwords.
π Encrypt single file #
DownloadMediaBuilder(
url: "https://example.com/sample.jpg",
encryptionPassword: "this is a password",
onSuccess: (snapshot) {
return Image.file(File(snapshot.filePath!));
}
)
β³ File Expiration (Optional) #
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.
β‘ General Usage #
DownloadMediaBuilder(
url: 'https://example.com/sample.jpg',
onSuccess: (snapshot) {
return Image.file(File(snapshot.filePath!));
},
onLoading: (snapshot) {
return LinearProgressIndicator(value: snapshot.progress);
},
),
β―οΈ Disable Auto Download #
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),
),
β οΈ Handle Loading & Error States #
DownloadMediaBuilder(
url: 'https://example.com/video.mp4',
onLoading: (snapshot) => LinearProgressIndicator(value: snapshot.progress),
onSuccess: (snapshot) => BetterPlayer.file(snapshot.filePath!),
onError: (_) => const Text('β Error!'),
),
βΉοΈ Cancel & π Retry Downloads #
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:
- β You can only call cancel() if status =
loading
. - π You can only call retry() if status =
canceled
.
π Snapshot Explained #
DownloadMediaSnapshot
contains 3 fields:
- Status β
Initial
,Success
,Loading
,Error
,Canceled
,Encrypting
,Decrypting
. - FilePath β Available if file is downloaded.
- Progress β Download progress (0 β 1).