From c2a14240ada24cd7c816bcb1ac942986eb855792 Mon Sep 17 00:00:00 2001 From: lovegaoshi <106490582+lovegaoshi@users.noreply.github.com> Date: Tue, 28 May 2024 00:29:21 -0700 Subject: [PATCH] fix(android): android cache header (#3832) * fix: android cache header --- .../exoplayer/ReactExoplayerSimpleCache.kt | 16 +++++++++------- .../exoplayer/ReactExoplayerView.java | 17 ++++++++--------- examples/basic/src/VideoPlayer.tsx | 1 + 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerSimpleCache.kt b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerSimpleCache.kt index 23d51f7793..c644ccb2ec 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerSimpleCache.kt +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerSimpleCache.kt @@ -12,10 +12,9 @@ import java.io.File object RNVSimpleCache { // TODO: when to release? how to check if cache is released? private var simpleCache: SimpleCache? = null - var cacheDataSourceFactory: DataSource.Factory? = null - fun setSimpleCache(context: Context, cacheSize: Int, factory: HttpDataSource.Factory) { - if (cacheDataSourceFactory != null || cacheSize <= 0) return + fun setSimpleCache(context: Context, cacheSize: Int) { + if (simpleCache != null || cacheSize <= 0) return simpleCache = SimpleCache( File(context.cacheDir, "RNVCache"), LeastRecentlyUsedCacheEvictor( @@ -23,9 +22,12 @@ object RNVSimpleCache { ), StandaloneDatabaseProvider(context) ) - cacheDataSourceFactory = - CacheDataSource.Factory() - .setCache(simpleCache!!) - .setUpstreamDataSourceFactory(factory) + } + + fun getCacheFactory(factory: HttpDataSource.Factory): DataSource.Factory { + if (simpleCache == null) return factory + return CacheDataSource.Factory() + .setCache(simpleCache!!) + .setUpstreamDataSourceFactory(factory) } } diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index ee7d53e3f4..3cd479cd68 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -215,7 +215,7 @@ public class ReactExoplayerView extends FrameLayout implements private boolean selectTrackWhenReady = false; private Handler mainHandler; private Runnable mainRunnable; - private DataSource.Factory cacheDataSourceFactory; + private boolean useCache = false; private ControlsConfig controlsConfig = new ControlsConfig(); // Props from React @@ -709,8 +709,8 @@ private void initializePlayerCore(ReactExoplayerView self) { .setAdErrorListener(this) .build(); DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory); - if (cacheDataSourceFactory != null) { - mediaSourceFactory.setDataSourceFactory(cacheDataSourceFactory); + if (useCache) { + mediaSourceFactory.setDataSourceFactory(RNVSimpleCache.INSTANCE.getCacheFactory(buildHttpDataSourceFactory(true))); } if (adsLoader != null) { @@ -1016,13 +1016,13 @@ private MediaSource buildMediaSource(Uri uri, String overrideExtension, DrmSessi throw new IllegalStateException("cannot open input file" + srcUri); } } else if ("file".equals(srcUri.getScheme()) || - cacheDataSourceFactory == null) { + !useCache) { mediaSourceFactory = new ProgressiveMediaSource.Factory( mediaDataSourceFactory ); } else { mediaSourceFactory = new ProgressiveMediaSource.Factory( - cacheDataSourceFactory + RNVSimpleCache.INSTANCE.getCacheFactory(buildHttpDataSourceFactory(true)) ); } @@ -2241,12 +2241,11 @@ public void setBufferConfig(BufferConfig config) { if (bufferConfig.getCacheSize() > 0) { RNVSimpleCache.INSTANCE.setSimpleCache( this.getContext(), - bufferConfig.getCacheSize(), - buildHttpDataSourceFactory(false) + bufferConfig.getCacheSize() ); - cacheDataSourceFactory = RNVSimpleCache.INSTANCE.getCacheDataSourceFactory(); + useCache = true; } else { - cacheDataSourceFactory = null; + useCache = false; } releasePlayer(); initializePlayer(); diff --git a/examples/basic/src/VideoPlayer.tsx b/examples/basic/src/VideoPlayer.tsx index aafd85427e..b5fae5fb8c 100644 --- a/examples/basic/src/VideoPlayer.tsx +++ b/examples/basic/src/VideoPlayer.tsx @@ -180,6 +180,7 @@ class VideoPlayer extends Component { { description: 'another bunny (can be saved)', uri: 'https://rawgit.com/mediaelement/mediaelement-files/master/big_buck_bunny.mp4', + headers: {referer: 'www.github.com', 'User-Agent': 'react.native.video'}, }, { description: 'sintel with subtitles',