搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。