# 媒体播放器缓存 (/zh-CN/api-reference/rtc/cpp-all-platforms/play/mediaplayer/mediaplayer-cache) > For AI agents: see the complete documentation index at [llms.txt](/llms.txt). ## enableAutoRemoveCache [#enableautoremovecache] 设置是否自动删除缓存的媒体文件。 ```cpp virtual int enableAutoRemoveCache(bool enable) = 0; ``` 启用该功能后,如果缓存的媒体文件数量或大小超过设置的限制,SDK 会自动删除最近最少使用的缓存文件。 ### 参数 [#参数] 是否启用 SDK 自动删除缓存的媒体文件: - true:自动删除缓存的媒体文件。 - false:(默认)不自动删除缓存的媒体文件。 ### 返回值 [#返回值] - 0:方法调用成功。 - < 0:方法调用失败,详见 [MEDIA_PLAYER_REASON](/zh-CN/api-reference/rtc/cpp-all-platforms/enum-mediaplayerreason)。 ### 所属接口类 [#所属接口类] - [IMediaPlayerCacheManager](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_imediaplayercachemanager) ## getCacheDir [#getcachedir] 获取待缓存媒体文件的存储路径。 ```cpp virtual int getCacheDir(char* path, int length) = 0; ``` 如果在调用该方法前未调用 [setCacheDir](/zh-CN/api-reference/rtc/cpp-all-platforms/play/mediaplayer/mediaplayer-cache#api_imediaplayercachemanager_setcachedir) 设置待缓存媒体文件的存储路径,则该方法返回 SDK 使用的默认存储路径。 ### 参数 [#参数-1] 输出参数,待缓存媒体文件的存储路径。 `path` 缓存文件存储路径字符串的最大长度。 ### 返回值 [#返回值-1] - 0:方法调用成功。 - < 0:方法调用失败,详见 [MEDIA_PLAYER_REASON](/zh-CN/api-reference/rtc/cpp-all-platforms/enum-mediaplayerreason)。 ### 所属接口类 [#所属接口类-1] - [IMediaPlayerCacheManager](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_imediaplayercachemanager) ## getCacheFileCount [#getcachefilecount] 获取已缓存的媒体文件数量。 ```cpp virtual int getCacheFileCount() = 0; ``` ### 返回值 [#返回值-2] - 方法调用成功,返回已缓存的媒体文件数量。 - < 0:方法调用失败,参见 [MEDIA_PLAYER_REASON](/zh-CN/api-reference/rtc/cpp-all-platforms/enum-mediaplayerreason)。 ### 所属接口类 [#所属接口类-2] - [IMediaPlayerCacheManager](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_imediaplayercachemanager) ## getMaxCacheFileCount [#getmaxcachefilecount] 获取可缓存的媒体文件的最大数量。 ```cpp virtual int getMaxCacheFileCount() = 0; ``` ### 返回值 [#返回值-3] - 0:方法调用成功。 - < 0:方法调用失败,详见 [MEDIA_PLAYER_REASON](/zh-CN/api-reference/rtc/cpp-all-platforms/enum-mediaplayerreason)。 ### 所属接口类 [#所属接口类-3] - [IMediaPlayerCacheManager](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_imediaplayercachemanager) ## getMaxCacheFileSize [#getmaxcachefilesize] 获取缓存媒体文件聚合存储空间的最大大小。 ```cpp virtual int64_t getMaxCacheFileSize() = 0; ``` 你可以调用 [setMaxCacheFileSize](/zh-CN/api-reference/rtc/cpp-all-platforms/play/mediaplayer/mediaplayer-cache#api_imediaplayercachemanager_setmaxcachefilesize) 方法设置缓存媒体文件聚合存储空间的最大大小,默认值为 1 GB。 ### 返回值 [#返回值-4] - 方法调用成功,返回缓存媒体文件聚合存储空间的最大大小(单位为字节)。 - < 0:方法调用失败,详见 [MEDIA_PLAYER_REASON](/zh-CN/api-reference/rtc/cpp-all-platforms/enum-mediaplayerreason)。 ### 所属接口类 [#所属接口类-4] - [IMediaPlayerCacheManager](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_imediaplayercachemanager) ## removeAllCaches [#removeallcaches] 删除媒体播放器中所有缓存的媒体文件。 ```cpp virtual int removeAllCaches() = 0; ``` :::note 注: [removeAllCaches](/zh-CN/api-reference/rtc/cpp-all-platforms/play/mediaplayer/mediaplayer-cache#api_imediaplayercachemanager_removeallcaches) 不会删除当前正在播放的缓存媒体文件。 ::: ### 返回值 [#返回值-5] - 0:方法调用成功。 - < 0:方法调用失败,详见 [MEDIA_PLAYER_REASON](/zh-CN/api-reference/rtc/cpp-all-platforms/enum-mediaplayerreason)。 ### 所属接口类 [#所属接口类-5] - [IMediaPlayerCacheManager](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_imediaplayercachemanager) ## removeCacheByUri [#removecachebyuri] 删除缓存的媒体文件。 ```cpp virtual int removeCacheByUri(const char *uri) = 0; ``` :::note 注: [removeCacheByUri](/zh-CN/api-reference/rtc/cpp-all-platforms/play/mediaplayer/mediaplayer-cache#api_imediaplayercachemanager_removecachebyuri) 当前不会删除正在播放的缓存媒体文件。 ::: ### 参数 [#参数-2] 要删除的媒体文件的 URI(统一资源标识符)。 ### 返回值 [#返回值-6] - 0:方法调用成功。 - < 0:方法调用失败,详见 [MEDIA_PLAYER_REASON](/zh-CN/api-reference/rtc/cpp-all-platforms/enum-mediaplayerreason)。 ### 所属接口类 [#所属接口类-6] - [IMediaPlayerCacheManager](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_imediaplayercachemanager) ## removeOldCache [#removeoldcache] 删除最少使用的缓存媒体文件。 ```cpp virtual int removeOldCache() = 0; ``` 当缓存文件存储空间即将达到上限时,你可以调用该方法,SDK 会删除最少使用的缓存媒体文件。 :::note 注: [removeOldCache](/zh-CN/api-reference/rtc/cpp-all-platforms/play/mediaplayer/mediaplayer-cache#api_imediaplayercachemanager_removeoldcache) 不会删除当前正在播放的缓存媒体文件。 ::: ### 返回值 [#返回值-7] - 0:方法调用成功。 - < 0:方法调用失败,详见 [MEDIA_PLAYER_REASON](/zh-CN/api-reference/rtc/cpp-all-platforms/enum-mediaplayerreason)。 ### 所属接口类 [#所属接口类-7] - [IMediaPlayerCacheManager](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_imediaplayercachemanager) ## setCacheDir [#setcachedir] 设置要缓存的媒体文件的存储路径。 ```cpp virtual int setCacheDir(const char *path) = 0; ``` :::note 注: 请确保在调用该方法前已初始化 [IRtcEngine](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_irtcengine)。 ::: ### 参数 [#参数-3] 要缓存的媒体文件的绝对路径。 :::note 注: 确保该目录已存在且可写。 ::: ### 返回值 [#返回值-8] - 0:方法调用成功。 - < 0:方法调用失败,详见 [MEDIA_PLAYER_REASON](/zh-CN/api-reference/rtc/cpp-all-platforms/enum-mediaplayerreason)。 ### 所属接口类 [#所属接口类-8] - [IMediaPlayerCacheManager](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_imediaplayercachemanager) ## setMaxCacheFileCount [#setmaxcachefilecount] 设置可缓存的媒体文件的最大数量。 ```cpp virtual int setMaxCacheFileCount(int count) = 0; ``` ### 参数 [#参数-4] 可缓存的媒体文件的最大数量。默认值为 1000。 ### 返回值 [#返回值-9] - 0:方法调用成功。 - < 0:方法调用失败,详见 [MEDIA_PLAYER_REASON](/zh-CN/api-reference/rtc/cpp-all-platforms/enum-mediaplayerreason)。 ### 所属接口类 [#所属接口类-9] - [IMediaPlayerCacheManager](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_imediaplayercachemanager) ## setMaxCacheFileSize [#setmaxcachefilesize] 设置缓存媒体文件的聚合存储空间的最大大小。 ```cpp virtual int setMaxCacheFileSize(int64_t cacheSize) = 0; ``` ### 参数 [#参数-5] 缓存媒体文件的聚合存储空间的最大大小(字节)。默认值为 1 GB。 ### 返回值 [#返回值-10] - 0:方法调用成功。 - < 0:方法调用失败,详见 [MEDIA_PLAYER_REASON](/zh-CN/api-reference/rtc/cpp-all-platforms/enum-mediaplayerreason)。 ### 所属接口类 [#所属接口类-10] - [IMediaPlayerCacheManager](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_imediaplayercachemanager) ## onPlayerCacheStats [#onplayercachestats] 上报正在缓存的媒体文件的统计信息。 ```cpp virtual void onPlayerCacheStats(const media::base::CacheStatistics& stats) ``` 调用 [openWithMediaSource](/zh-CN/api-reference/rtc/cpp-all-platforms/play/mediaplayer/mediaplayer-open#api_imediaplayer_openwithmediasource) 方法并将 `enableCache` 设置为 true 后,SDK 每秒触发一次该回调,上报正在缓存的媒体文件的统计信息。 ### 参数 [#参数-6] 媒体文件缓存统计信息。详见 [CacheStatistics](/zh-CN/api-reference/rtc/cpp-all-platforms/class-cachestatistics)。 ### 所属接口类 [#所属接口类-11] - [IMediaPlayerSourceObserver](/zh-CN/api-reference/rtc/cpp-all-platforms/rtc-interface-class#class_imediaplayersourceobserver)