• Android Media3 ExoPlayer 开启缓存功能


        ExoPlayer 开启播放缓存功能,在下次加载已经播放过的网络资源的时候,可以直接从本地缓存加载,实现为用户节省流量和提升加载效率的作用。

    方法一:采用 ExoPlayer 缓存策略

    第 1 步:实现 Exoplayer

    参考 Exoplayer 官网 Release notes :

    对应关系:

    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 从 2.19.0 开始迁移至 AndroidX 的 Media3 框架内,2.19.1 是 Exoplayer 作为独立项目发布的最后一个版本,所以引入 Exoplayer 2.19.1 有以下两个方式,建议采用最新的方式 2。

    1. # 方式1
    2. implementation 'com.google.android.exoplayer:exoplayer-core:2.19.1'
    3. implementation 'com.google.android.exoplayer:exoplayer-dash:2.19.1'
    4. implementation 'com.google.android.exoplayer:exoplayer-ui:2.19.1'
    5. # 方式2
    6. implementation "androidx.media3:media3-exoplayer:1.1.1"
    7. implementation "androidx.media3:media3-exoplayer-dash:1.1.1"
    8. implementation "androidx.media3:media3-ui:1.1.1"

    第 2 步:在应用的 Application 类中创建缓存策略

    1. public class MyApplication extends Application {
    2. public SimpleCache simpleCache;
    3. public void onCreate() {
    4. super.onCreate();
    5. //缓存最大值为100M
    6. LeastRecentlyUsedCacheEvictor leastRecentlyUsedCacheEvictor = new
    7. LeastRecentlyUsedCacheEvictor(100 * 1024 * 1024);
    8. if (simpleCache == null)
    9. {
    10. simpleCache = new SimpleCache(getCacheDir(), leastRecentlyUsedCacheEvictor, new
    11. ExoDatabaseProvider(this));
    12. }
    13. }
    14. ...
    15. }

    第 3 步:加载数据源,实现缓存

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

    方法二:  通过 Android Video Cache Library

        开源库 AndroidVideoCache 的原理是通过代理的策略实现一个中间层,将网络视频请求转移到本地实现的代理服务器上,这样真正请求的数据就会被代理拿到,然后代理一边向本地写入数据,一边根据需要的数据看是读网络数据还是读本地缓存数据,从而实现数据的复用。

    第 1 步:实现 VideoCache

    implementation 'com.danikula:videocache:2.7.1'

    第 2 步:在应用程序类中存储共享代理

    1. public class MyApplication extends Application {
    2. private HttpProxyCacheServer proxy;
    3. public static HttpProxyCacheServer getProxy(Context context) {
    4. MyApplication app = (MyApplication) context.getApplicationContext();
    5. return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    6. }
    7. private HttpProxyCacheServer newProxy() {
    8. return new HttpProxyCacheServer.Builder(this)
    9. .maxCacheSize(1024 * 1024 * 1024)
    10. .build();
    11. }
    12. }

    第 3 步:Exoplayer 接入缓存

    1. HttpProxyCacheServer proxy = getProxy(activity);
    2. //注意应采用来自代理的 url 而不是原始 url 来添加缓存
    3. String proxyUrl = proxy.getProxyUrl(VIDEO_URL);
    4. PlayerView playerView = findViewById(R.id.video_view);
    5. ExoPlayer player = ExoPlayerFactory.newSimpleInstance(VideoActivity.this,
    6. new DefaultRenderersFactory(this),
    7. new DefaultTrackSelector());
    8. MediaSource mediaSource = buildMediaSource(proxyUrl);
    9. player.prepare(mediaSource, true, false);
    10. playerView.setPlayer(player);
  • 相关阅读:
    web-logic-ssrf内网渗透
    《JavaSE-第十九章》之Collection
    R语言发送邮件丨blastula包使用教程
    docker搭建私有仓库并推送本地镜像
    第一章 数据可视化和matplotlib
    Yarn工作机制流程
    java计算机毕业设计星之语明星周边产品销售网站源码+系统+lw+数据库+调试运行
    高等数学(第七版)同济大学 习题10-3 (前9题)个人解答
    红日靶场(内网渗透)——2
    KNN-K近邻算法(K-Nearest Neighbors)
  • 原文地址:https://blog.csdn.net/crazestone0614/article/details/132955293