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.
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 cache or preload only one MP4, MP3, FLV, or HLS file at a time.
- The local caching and preloading features support only video playback based on UrlSource. Video playback based on VidAuth and VidSts are not supported.
Use the local caching feature
How it works
After you call enableLocalCache
in the AliPlayerGlobalSettings
class, the local caching feature is enabled. Then, the player checks whether the
video can be cached and whether the cached content expires based on the request header.
You can set maxCapacityMB
to specify the maximum cache size and set freeStorageMB
to specify the minimum free space of the disk. This prevents excessive data from
occupying disk space.
ApsaraVideo Player SDK distinguishes videos based on playback URLs. However, the playback
URLs of a video may vary based on the authentication information. In this case, you
can call setCacheUrlHashCallback
to obtain the URL hash value, which is the unique ID for the video. The player then
can use this ID to identify the video.
For example, the playback URL http://****.mp4?authKey=aaa
contains authentication information. In this case, obtain the MD5 hash value of http://****.mp4
. The player can then identify the video even if the authentication information changes.
If the HTTP and HTTPS URLs point to the same file, you can configure the system to
return the same hash value. This way, the player plays 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.
References
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 by 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.