Android Media3 ExoPlayer turns on caching function

    ExoPlayer turns on the playback cache function. When loading network resources that have been played next time, it can be loaded directly from the local cache, saving users traffic and improving loading efficiency.

Method 1: Use ExoPlayer caching strategy

Step 1: Implement Exoplayer

Refer to Exoplayer official website Release notes :

Correspondence:

2.19.0 (2023-07-05)  -- AndroidX Media3 1.1.0 release.

2.19.1 (2023-08-14)  -- AndroidX Media3 1.1.1 release

    Exoplayer has been migrated to the Media3 framework of AndroidX starting from 2.19.0. 2.19.1 is the last version released by Exoplayer as an independent project. Therefore, there are two ways to introduce Exoplayer 2.19.1. It is recommended to use the latest method 2.

# 方式1
implementation 'com.google.android.exoplayer:exoplayer-core:2.19.1'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.19.1'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.19.1'

# 方式2
implementation "androidx.media3:media3-exoplayer:1.1.1"
implementation "androidx.media3:media3-exoplayer-dash:1.1.1"
implementation "androidx.media3:media3-ui:1.1.1"

Step 2: Create a cache policy in the application class

public SimpleCache simpleCache;
@Override
    public void onCreate() {
        super.onCreate();
        //缓存最大值为100M
        LeastRecentlyUsedCacheEvictor leastRecentlyUsedCacheEvictor = new LeastRecentlyUsedCacheEvictor(100 * 1024 * 1024);
        if (simpleCache == null) {
            simpleCache = new SimpleCache(getCacheDir(), leastRecentlyUsedCacheEvictor, new ExoDatabaseProvider(this));
        }
}

Step 3: Load the data source and implement caching

//本地资源(如:/sdcard/media/1.mp4)或 HTTP 资源
Uri videoUri = Uri.parse("YOUR URL");
MediaItem mediaItem = MediaItem.fromUri(videoUri);
DefaultHttpDataSource.Factory httpDataSourceFactory = new DefaultHttpDataSource.Factory().setAllowCrossProtocolRedirects(true);
// 这里的DefaultDataSource同时支持本地和HTTP请求的资源,自动实现检测 (The DefaultDataSource supports both local and Http sources. It automatically detects which one to use.)
DefaultDataSource.Factory defaultDataSourceFactory = new DefaultDataSourceFactory(requireContext(), httpDataSourceFactory);
//实现缓存
CacheDataSource.Factory cacheDataSourceFactory = new CacheDataSource.Factory()
                .setCache(MyApplication.getAppInstance().simpleCache)
                .setUpstreamDataSourceFactory(defaultDataSourceFactory)
                .setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR);

MediaSource mediaSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory)
                .createMediaSource(mediaItem);
player.setMediaSource(mediaSource, true);

Method 2:   Through Android Video Cache Library

 The principle of the open source library AndroidVideoCache is to implement a middle layer through the proxy strategy to transfer network video requests to the locally implemented proxy server, so that the actual requested data will be obtained by the proxy, and then the proxy will write data     locally  while Depending on the required data, it depends on whether to read network data or local cache data, thereby realizing data reuse.

Step 1: Implement VideoCache

implementation 'com.danikula:videocache:2.7.1'

Step 2: Store the shared proxy in the application class

public class MyApplication extends Application {

        private HttpProxyCacheServer proxy;

        public static HttpProxyCacheServer getProxy(Context context) {
            MyApplication app = (MyApplication) context.getApplicationContext();
            return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
        }
    
        private HttpProxyCacheServer newProxy() {
            return new HttpProxyCacheServer.Builder(this)
                    .maxCacheSize(1024 * 1024 * 1024)
                    .build();
    
        }

}

Step 3: Exoplayer access cache

HttpProxyCacheServer proxy = getProxy(activity);
//注意应采用来自代理的 url 而不是原始 url 来添加缓存
String proxyUrl = proxy.getProxyUrl(VIDEO_URL);
PlayerView playerView = findViewById(R.id.video_view);
ExoPlayer player = ExoPlayerFactory.newSimpleInstance(VideoActivity.this,
                new DefaultRenderersFactory(this),
                new DefaultTrackSelector());
MediaSource mediaSource = buildMediaSource(proxyUrl);
player.prepare(mediaSource, true, false);
playerView.setPlayer(player);

Guess you like

Origin blog.csdn.net/crazestone0614/article/details/132955293