Skip to main content

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,
/// (Android only) 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.
important

On Android, providing a unique key is essential if you want the cached data to be available after the application is closed and reopened. Without a key, the cache may be treated as session-only.

Cache Management

Clear All Cache

To remove all cached data from the device:

betterPlayerController.clearCache();

Pre-Caching

You can start downloading a video into the cache before playback begins:

betterPlayerController.preCache(_betterPlayerDataSource);

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, HLSCachingReverseProxyServer is used for HLS streams, and CachingPlayerItem is used for other formats.

FeatureAndroid HLSAndroid non-HLSiOS HLSiOS non-HLS
Normal Caching
Pre-Cachingx
Stop Cachingx