ApsaraVideo Player SDK provides the local caching and preloading features. You can use these features to accelerate the startup of video playback and shorten the response time after you drag the progress handle. This improves playback performance.
For the code and implementation details of all features described in this topic, we recommend that you refer to the API-Example project and adapt or extend it based on best practices.
For a specific implementation, you can use the configurations described in this topic and refer to the source code of the corresponding API-Example-Android and API-Example-iOS Video Preloading (Direct URL) modules.
Background information
If you play a video over the Internet on repeat, each request consumes data and stuttering may occur during video playback. ApsaraVideo Player SDK provides the local caching and preloading features for you to accelerate the startup of video playback and shorten the response time after you drag the progress handle. This reduces video stuttering.
Limits
You can preload only one MP4, MP3, FLV, or HLS (single-bitrate video stream) file at a time.
The preloading feature supports only video playback based on UrlSource. Video playback based on VidAuth or VidSts is not supported.
Use the local caching feature
How it works
You can enable the local caching feature of ApsaraVideo Player SDK using the global switch enableLocalCache in AliPlayerGlobalSettings. After you enable this feature, the player automatically determines whether a video can be cached and whether the cache has expired based on the information in the HTTP header. You can also configure the maximum cache capacity (maxCapacityMB) and the minimum free disk space (freeStorageMB) to prevent the cache from occupying excessive disk space.
ApsaraVideo Player SDK distinguishes videos based on their playback URLs. However, some URLs contain authentication parameters, which causes the playback URL for the same video to vary with each request. To handle this scenario, ApsaraVideo Player SDK provides the URL hash callback (setCacheUrlHashCallback). The player uses the return value of this callback to distinguish between videos. This return value can be considered the unique ID of the video.
For example, for a URL with authentication parameters, such as http://****.mp4?authKey=aaa, you can use the MD5 value of http://****.mp4 as the return value in the callback. This way, changes in the authentication parameters do not affect video identification. For scenarios where HTTP and HTTPS point to the same address, you can also return the same value to allow the player to use the same cache file.
Scenarios
This feature is suitable for scenarios in which you need to play a video on repeat. For example, loop playback of a short video.
Configuration method
Use the preloading feature
How it works
The preloading feature is an advanced feature developed based on local caching. This feature caches the next video during the playback of the current video. This accelerates the playback startup of the next video. You can preload the next video using another player instance to improve the playback of videos in a playlist. After you enable the local caching feature, you can use another player instance to cache the next video during the playback of the current video when sufficient content is buffered for the current video. This way, the next video can be immediately played after the playback of the current video ends. After sufficient content is buffered for the next video, you can cache the following video in the playlist to provide more video cache.
Scenarios
This feature is suitable for playlist playback.