为 exoplayer 启用缓存

Aka*_*jhi 4 video android caching http-live-streaming exoplayer

我已经为 hls 视频链接实现了 exoplayer,一旦视频播放,它会再次加载视频进行播放,任何人都可以建议如何在视频完全流式传输后再次停止加载并在不缓冲的情况下播放。如何存储hls流媒体视频的缓存。如果有的话请提供解决方案。提前致谢 :)

 TrackSelector trackSelector = new DefaultTrackSelector(this);

    DefaultLoadControl loadControl = new DefaultLoadControl.Builder()
            .setBufferDurationsMs(1024, 64 * 1024, 1024, 1024)
            .createDefaultLoadControl();


    videoView = findViewById(R.id.video_view);
    player = ExoPlayerFactory.newSimpleInstance(this, trackSelector,loadControl);
Run Code Online (Sandbox Code Playgroud)

// 玩家 = ExoPlayerFactory.newSimpleInstance(this);

    player.setPlayWhenReady(true);
    videoView.setPlayer(player);

    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
    Util.getUserAgent(this, "ExoPlayer"));

// Produces Extractor instances for parsing the media data.
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();


    MediaSource mediaSource = new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(videoUrl));

    player.prepare(mediaSource);
    player.setPlayWhenReady(true);
Run Code Online (Sandbox Code Playgroud)

Tus*_*iya 7

方法一:使用Exoplayer缓存策略

第 1 步:实施 Exoplayer

implementation 'com.google.android.exoplayer:exoplayer-core:2.15.0'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.15.0'
Run Code Online (Sandbox Code Playgroud)

步骤 2:在您的 Application 类中创建缓存策略

public SimpleCache simpleCache;
@Override
    public void onCreate() {
        super.onCreate();

        LeastRecentlyUsedCacheEvictor leastRecentlyUsedCacheEvictor = new LeastRecentlyUsedCacheEvictor(100 * 1024 * 1024);
        if (simpleCache == null) {
            simpleCache = new SimpleCache(getCacheDir(), leastRecentlyUsedCacheEvictor, new ExoDatabaseProvider(this));
        }
}
Run Code Online (Sandbox Code Playgroud)

步骤3:像下面的方法一样缓存内容

Uri videoUri = Uri.parse("YOUR URL");
        MediaItem mediaItem = MediaItem.fromUri(videoUri);
        DefaultHttpDataSource.Factory httpDataSourceFactory = new DefaultHttpDataSource.Factory().setAllowCrossProtocolRedirects(true);
        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);
Run Code Online (Sandbox Code Playgroud)

方法 2: Android 视频缓存库完全可以满足您的需求。请按照以下步骤缓存您的视频。

步骤1 :implementation 'com.danikula:videocache:2.7.1'

步骤 2:将共享代理存储在您的 Application 类中

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();
            //return new HttpProxyCacheServer(this);
    
        }
}
Run Code Online (Sandbox Code Playgroud)

步骤 3:将 MyApplication 类放入清单文件中,例如

<application
        android:name=". MyApplication">
.
.
.
</application>
Run Code Online (Sandbox Code Playgroud)

步骤 4:使用代理中的 url 而不是原始 url 来添加缓存

HttpProxyCacheServer proxy = MyApplication.getProxy(activity);
            String proxyUrl = proxy.getProxyUrl(VIDEO_URL);
            videoView.setVideoPath(proxyUrl);
Run Code Online (Sandbox Code Playgroud)

如果您使用的是 exoplayer

HttpProxyCacheServer proxy = getProxy(activity);
                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);
Run Code Online (Sandbox Code Playgroud)

快乐编码:)