ApsaraVideo Player SDK provides local cache and preloading features that speed up video startup and improve seek responsiveness.
For the code and implementation details of all features in this topic, refer to the API-Example sample project. You can adapt and extend the project based on best practices.
For specific implementations, refer to the configuration methods described below and the source code of the Video Preloading (Direct URL) module in the corresponding API-Example-Android and API-Example-iOS sample projects.
Background information
When users watch videos repeatedly, fetching each playback from the network wastes bandwidth and increases the risk of stuttering. ApsaraVideo Player SDK local cache and preloading features significantly improve startup speed and seek responsiveness, reducing playback stuttering.
Use local cache to improve playback experience
How it works
Enable local caching through the global AliPlayerGlobalSettings switch enableLocalCache. Once enabled, the player automatically determines whether a video can be cached and whether the cache has expired based on HTTP header information. You do not need to manage this process. To prevent excessive disk usage, configure the maximum cache size (maxCapacityMB) and the required minimum free disk space (freeStorageMB).
The player distinguishes videos by their playback URLs. Because some URLs contain authentication parameters that change between sessions, ApsaraVideo Player SDK provides the URL hash callback (setCacheUrlHashCallback). The player uses the callback return value as the unique identifier for each video.
For example, given a URL such as http://****.mp4?authKey=aaa, you can return the MD5 value of http://****.mp4 in the callback so that changing authentication parameters does not affect video identification. Similarly, if HTTP and HTTPS URLs point to the same video, return the same value to ensure the player uses the same cache file.
Scenarios
Suitable for repeated video viewing scenarios, such as loop playback in short video applications.
Configuration
Use preloading to improve playback experience
How it works
Preloading extends local caching by loading the next video into the cache while the current video plays, enabling instant startup on switch. For list playback with fast scrolling, combine preloading with dual player instances: enable local cache, then use a second player instance to prepare the next video while the current one has a sufficient buffer. When the user scrolls to the next video, playback starts immediately. After the second instance buffer is full, continue preloading subsequent videos to grow the cache.
Scenarios
Suitable for list playback scenarios with fast scrolling.