fix(android): android cache header (#3832)

* fix: android cache header
This commit is contained in:
lovegaoshi 2024-05-28 00:29:21 -07:00 committed by GitHub
parent 5c29b48747
commit c2a14240ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 16 deletions

View File

@ -12,10 +12,9 @@ import java.io.File
object RNVSimpleCache { object RNVSimpleCache {
// TODO: when to release? how to check if cache is released? // TODO: when to release? how to check if cache is released?
private var simpleCache: SimpleCache? = null private var simpleCache: SimpleCache? = null
var cacheDataSourceFactory: DataSource.Factory? = null
fun setSimpleCache(context: Context, cacheSize: Int, factory: HttpDataSource.Factory) { fun setSimpleCache(context: Context, cacheSize: Int) {
if (cacheDataSourceFactory != null || cacheSize <= 0) return if (simpleCache != null || cacheSize <= 0) return
simpleCache = SimpleCache( simpleCache = SimpleCache(
File(context.cacheDir, "RNVCache"), File(context.cacheDir, "RNVCache"),
LeastRecentlyUsedCacheEvictor( LeastRecentlyUsedCacheEvictor(
@ -23,9 +22,12 @@ object RNVSimpleCache {
), ),
StandaloneDatabaseProvider(context) StandaloneDatabaseProvider(context)
) )
cacheDataSourceFactory = }
CacheDataSource.Factory()
.setCache(simpleCache!!) fun getCacheFactory(factory: HttpDataSource.Factory): DataSource.Factory {
.setUpstreamDataSourceFactory(factory) if (simpleCache == null) return factory
return CacheDataSource.Factory()
.setCache(simpleCache!!)
.setUpstreamDataSourceFactory(factory)
} }
} }

View File

@ -215,7 +215,7 @@ public class ReactExoplayerView extends FrameLayout implements
private boolean selectTrackWhenReady = false; private boolean selectTrackWhenReady = false;
private Handler mainHandler; private Handler mainHandler;
private Runnable mainRunnable; private Runnable mainRunnable;
private DataSource.Factory cacheDataSourceFactory; private boolean useCache = false;
private ControlsConfig controlsConfig = new ControlsConfig(); private ControlsConfig controlsConfig = new ControlsConfig();
// Props from React // Props from React
@ -709,8 +709,8 @@ public class ReactExoplayerView extends FrameLayout implements
.setAdErrorListener(this) .setAdErrorListener(this)
.build(); .build();
DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory); DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory);
if (cacheDataSourceFactory != null) { if (useCache) {
mediaSourceFactory.setDataSourceFactory(cacheDataSourceFactory); mediaSourceFactory.setDataSourceFactory(RNVSimpleCache.INSTANCE.getCacheFactory(buildHttpDataSourceFactory(true)));
} }
if (adsLoader != null) { if (adsLoader != null) {
@ -1016,13 +1016,13 @@ public class ReactExoplayerView extends FrameLayout implements
throw new IllegalStateException("cannot open input file" + srcUri); throw new IllegalStateException("cannot open input file" + srcUri);
} }
} else if ("file".equals(srcUri.getScheme()) || } else if ("file".equals(srcUri.getScheme()) ||
cacheDataSourceFactory == null) { !useCache) {
mediaSourceFactory = new ProgressiveMediaSource.Factory( mediaSourceFactory = new ProgressiveMediaSource.Factory(
mediaDataSourceFactory mediaDataSourceFactory
); );
} else { } else {
mediaSourceFactory = new ProgressiveMediaSource.Factory( mediaSourceFactory = new ProgressiveMediaSource.Factory(
cacheDataSourceFactory RNVSimpleCache.INSTANCE.getCacheFactory(buildHttpDataSourceFactory(true))
); );
} }
@ -2241,12 +2241,11 @@ public class ReactExoplayerView extends FrameLayout implements
if (bufferConfig.getCacheSize() > 0) { if (bufferConfig.getCacheSize() > 0) {
RNVSimpleCache.INSTANCE.setSimpleCache( RNVSimpleCache.INSTANCE.setSimpleCache(
this.getContext(), this.getContext(),
bufferConfig.getCacheSize(), bufferConfig.getCacheSize()
buildHttpDataSourceFactory(false)
); );
cacheDataSourceFactory = RNVSimpleCache.INSTANCE.getCacheDataSourceFactory(); useCache = true;
} else { } else {
cacheDataSourceFactory = null; useCache = false;
} }
releasePlayer(); releasePlayer();
initializePlayer(); initializePlayer();

View File

@ -180,6 +180,7 @@ class VideoPlayer extends Component {
{ {
description: 'another bunny (can be saved)', description: 'another bunny (can be saved)',
uri: 'https://rawgit.com/mediaelement/mediaelement-files/master/big_buck_bunny.mp4', 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', description: 'sintel with subtitles',