搜索插件、代码仓与 APP 分类

插件详情v0.3.1

media_cache_manager

此包可帮助您缓存媒体(音频、视频、图片等)。

AndroidiOSmacOSWindowsLinux
近 30 天下载
84
喜欢数
84
Pub 分数
160

安装插件

flutter pub add media_cache_manager

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

media_cache_manager 包提供了一种简单高效的方法,可在多个平台上缓存媒体文件,如图片、音频和视频。它可处理下载、存储和检索媒体,并自动清理过期或未使用的文件。该包设计注重性能和可靠性,支持同步和异步操作,非常适合需要从远程源频繁加载媒体内容的应用程序。该包与 Flutter 的资源系统无缝集成,确保设备存储的最优使用。

适合场景

  • 在图像画廊中缓存图像
  • 流式传输音频和视频内容
  • 减少重复媒体的网络请求
  • 移动应用中的离线媒体访问
  • 高效管理大型媒体资产

主要特点

  • 支持多种媒体类型(图像、音频、视频)
  • 缓存文件的自动过期和清理
  • 跨平台兼容性(Android、iOS、macOS、Windows、Linux)
  • 简单的 API 用于下载和检索
  • 可配置的缓存大小和保留策略

适合对象

  • 包含大量媒体内容的应用程序
  • 离线优先的应用程序
  • 媒体流媒体平台
  • 图片密集型社交媒体应用
  • 需要高效资源管理的应用程序

使用建议

  • 需要仔细管理缓存大小,以避免存储问题
  • 在某些平台上可能需要额外的文件系统访问权限
  • 未针对非常大的媒体文件进行优化,除非进行自定义配置
  • 有限的内置分析或监控工具
  • 依赖外部依赖项以实现高级功能

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。


📦 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
  1. Open android → app → build.gradle
  2. 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:

  1. Status → Initial, Success, Loading, Error, Canceled, Encrypting, Decrypting.
  2. FilePath → Available if file is downloaded.
  3. Progress → Download progress (0 → 1).

所属分类

依赖

依赖清单 4 项

源码仓库

使用此插件的项目

暂无相关数据。