MediaPlayerCacheManager

This class provides methods for managing cached media files in the media player.

enableAutoRemoveCache

Enables or disables the automatic cache file removal feature.

Future<void> enableAutoRemoveCache(bool enable);

When automatic cache file removal is enabled, if the number or total size of cached media files in the player exceeds the set limit, the SDK automatically removes the least recently used cache file.

Parameters

enable
Whether to enable automatic cache file removal:
  • true: Enables automatic cache file removal.
  • false: (Default) Disables automatic cache file removal.

Return Values

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly. See Error Codes for details and resolution suggestions.

getCacheDir

Gets the storage path of the cached files.

Future<String> getCacheDir(int length);

If you have not called the setCacheDir method to customize the cache file storage path before calling this method, the method returns the SDK's default cache file storage path.

Parameters

length
Input parameter. The maximum length of the cache file storage path string.

Return Values

  • On success, returns the storage path of the cached files.
  • < 0: Failure. See MediaPlayerReason.

getCacheFileCount

Gets the total number of currently cached media files.

Future<int> getCacheFileCount();

Return Values

  • ≥ 0: Success. Returns the total number of currently cached media files.
  • < 0: Failure. See MediaPlayerReason.

getMaxCacheFileCount

Gets the maximum number of cache files set.

Future<int> getMaxCacheFileCount();

The default maximum number of cache files in the SDK is 1000.

Return Values

  • > 0: Success. Returns the maximum number of cache files.
  • < 0: Failure. See MediaPlayerReason.

getMaxCacheFileSize

Gets the maximum total cache size of cache files set.

Future<int> getMaxCacheFileSize();

The default maximum total cache size of cache files in the SDK is 1GB. You can call the setMaxCacheFileSize method to customize the limit.

Return Values

  • > 0: Success. Returns the maximum total cache size in bytes.
  • < 0: Failure. See MediaPlayerReason.

removeAllCaches

Deletes all cached media files in the media player.

Future<void> removeAllCaches();
Note: This method does not delete cached media files that are currently playing.

Return Values

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly. See Error Codes for details and resolution suggestions.

removeCacheByUri

Deletes a specified cached media file.

uture<void> removeCacheByUri(String uri);
Note: This method does not delete cached media files that are currently playing.

Parameters

uri
The URI (Uniform Resource Identifier) of the cache file to be deleted, which can be used to identify the media file.

Return Values

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly. See Error Codes for details and resolution suggestions.

removeOldCache

Deletes the least recently used cached media file in the media player.

Future<void> removeOldCache();

When cached media files occupy too much space, you can call this method to clean up cache files. After calling this method, the SDK deletes the least recently used cached media file.

Note: When you call this method to delete cached media files, the currently playing cached media file will not be deleted.

Return Values

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly. See Error Codes for details and resolution suggestions.

setCacheDir

Sets the storage path for media files to be cached.

Future<void> setCacheDir(String path);
Note: This method must be called after initializing RtcEngine.

Parameters

path
The absolute path for storing cached files. Make sure the specified directory exists and is writable.

Return Values

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly. See Error Codes for details and resolution suggestions.

setMaxCacheFileCount

Sets the maximum number of cached media files.

Future<void> setMaxCacheFileCount(int count);

Parameters

count
The maximum number of media files that can be cached. The default value is 1000.

Return Values

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly. See Error Codes for details and resolution suggestions.

setMaxCacheFileSize

Sets the upper limit of the total cache size for media files.

Future<void> setMaxCacheFileSize(int cacheSize);

Parameters

cacheSize
The total cache size limit for media files, in bytes. The default is 1 GB.

Return Values

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly. See Error Codes for details and resolution suggestions.