Cache Configuration
Better Player provides a powerful caching system for network-based data sources to improve playback performance and reduce bandwidth usage. Caching is configured using the CacheConfiguration class within the PlayerDataSource.
Basic Configuration
PlayerDataSource _betterPlayerDataSource = PlayerDataSource(
DataSourceType.network,
Constants.elephantDreamVideoUrl,
cacheConfiguration: CacheConfiguration(
useCache: true,
preCacheSize: 10 * 1024 * 1024,
maxCacheSize: 10 * 1024 * 1024,
maxCacheFileSize: 10 * 1024 * 1024,
/// Key to persist cache between application sessions
key: "uniqueCacheKey",
),
);
Configuration Parameters
useCache: Enables or disables caching for the data source.maxCacheSize: (Android only) The maximum total size of the cache on disk in bytes.maxCacheFileSize: (Android only) The maximum size allowed for an individual cached file in bytes.key: A unique identifier used to persist and reuse cached data across application sessions on Android, iOS, and macOS.
Provide a unique key per video (especially in lists) so cached segments are persisted across sessions and never collide between different data sources.
Cache Management
Clear All Cache
To remove all cached data from the device:
betterPlayerController.clearCache();
Pre-Caching
You can download video data into cache before playback begins. Use the onProgress callback to monitor download progress in real time:
await betterPlayerController.preCache(
_betterPlayerDataSource,
onProgress: (PreCacheProgress progress) {
print('Progress: ${progress.percent.toStringAsFixed(1)}%');
print('Downloaded: ${progress.bytesDownloaded} / ${progress.totalBytes} bytes');
},
);
Pre-Cache Progress Stream
You can also monitor all active pre-cache tasks across the application via the broadcast stream:
final subscription = betterPlayerController.preCacheProgressStream.listen(
(PreCacheProgress progress) {
print('${progress.url}: ${progress.percent}%');
},
);
PreCacheProgress Properties
The PreCacheProgress model includes:
url: URL of the video being cached.cacheKey: Optional cache key associated with the media.progress: Download fraction from0.0to1.0(or negative on error/indeterminate).bytesDownloaded: Number of bytes downloaded so far.totalBytes: Total bytes expected (-1if indeterminate or unknown).percent: Download percentage from0.0to100.0.isCompleted: Indicates whether pre-caching completed successfully (progress >= 1.0 && error == null).hasError: Indicates whether pre-caching failed (error != null || progress < -1.5).error: Optional error description when pre-caching fails.
Stop Pre-Caching
To cancel an ongoing pre-caching operation:
betterPlayerController.stopPreCache(_betterPlayerDataSource);
Platform Support
The underlying implementation varies by platform. Android uses ExoPlayer's internal caching mechanism. On iOS and macOS, CachingPlayerItem and local cache storage manage media caching. On Windows and Linux, libmpv manages demuxer cache and network buffering internally during playback. On Web, fine-grained caching control (preCache, clearCache) is not currently supported natively by the Shaka Player wrapper.
| Feature | Android HLS | Android non-HLS | iOS HLS | iOS non-HLS | macOS HLS | macOS non-HLS | Windows HLS | Windows non-HLS | Linux HLS | Linux non-HLS | Web |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Normal Caching | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | x |
| Pre-Caching | x | ✓ | x | ✓ | x | ✓ | x | x | x | x | x |
| Pre-Cache Progress | x | ✓ | x | ✓ | x | ✓ | x | x | x | x | x |
| Stop Caching | x | ✓ | x | ✓ | x | ✓ | x | x | x | x | x |
- Chunked Streams (HLS & DASH): Pre-caching operates on progressive media files (MP4, MKV, etc.). Pre-caching HLS (
.m3u8) and DASH (.mpd) is blocked and not supported. Normal caching during active playback still caches HLS segments on Android, iOS, macOS, Windows, and Linux. - Windows & Linux:
libmpvautomatically manages demuxer cache and network buffering during active playback. Offline pre-caching is not supported. - Web: Browsers control media caching directly through HTTP caching headers.