From 71dcf59650aa958cc9506a6c722d0031ef4d39d6 Mon Sep 17 00:00:00 2001 From: Page Asgardius Date: Tue, 4 Oct 2022 18:45:54 -0700 Subject: [PATCH] cache release --- .../page/s3manager/CacheDataSourceFactory.java | 12 +++++++----- .../asgardius/page/s3manager/VideoPlayer.java | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/asgardius/page/s3manager/CacheDataSourceFactory.java b/app/src/main/java/asgardius/page/s3manager/CacheDataSourceFactory.java index e6aa575..c932f1d 100644 --- a/app/src/main/java/asgardius/page/s3manager/CacheDataSourceFactory.java +++ b/app/src/main/java/asgardius/page/s3manager/CacheDataSourceFactory.java @@ -17,13 +17,15 @@ import java.io.File; public class CacheDataSourceFactory implements DataSource.Factory { private final Context context; private final DefaultDataSource.Factory defaultDatasourceFactory; - private final long maxFileSize, maxCacheSize; + private final long maxFileSize; + SimpleCache simpleCache; //DatabaseProvider databaseProvider = ExoDatabaseProvider(this); - public CacheDataSourceFactory(Context context, long maxCacheSize, long maxFileSize) { + public CacheDataSourceFactory(Context context, SimpleCache simpleCache, long maxFileSize) { super(); this.context = context; - this.maxCacheSize = maxCacheSize; + this.simpleCache = simpleCache; + //this.maxCacheSize = maxCacheSize; this.maxFileSize = maxFileSize; String userAgent = Util.getUserAgent(context, context.getString(R.string.app_name)); //DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); @@ -32,8 +34,8 @@ public class CacheDataSourceFactory implements DataSource.Factory { @Override public DataSource createDataSource() { - LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize); - SimpleCache simpleCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor); + //LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize); + //simpleCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor); return new CacheDataSource(simpleCache, defaultDatasourceFactory.createDataSource(), new FileDataSource(), new CacheDataSink(simpleCache, maxFileSize), CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null); diff --git a/app/src/main/java/asgardius/page/s3manager/VideoPlayer.java b/app/src/main/java/asgardius/page/s3manager/VideoPlayer.java index 155f529..7b8f20c 100644 --- a/app/src/main/java/asgardius/page/s3manager/VideoPlayer.java +++ b/app/src/main/java/asgardius/page/s3manager/VideoPlayer.java @@ -19,6 +19,10 @@ import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.ProgressiveMediaSource; import com.google.android.exoplayer2.ui.StyledPlayerView; import com.google.android.exoplayer2.upstream.HttpDataSource; +import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor; +import com.google.android.exoplayer2.upstream.cache.SimpleCache; + +import java.io.File; /** * An example full-screen activity that shows and hides the system UI (i.e. @@ -31,6 +35,8 @@ public class VideoPlayer extends AppCompatActivity { private WifiManager.WifiLock mWifiLock; private PowerManager.WakeLock mWakeLock; private PowerManager powerManager; + private long maxCacheSize = 100 * 1024 * 1024; + SimpleCache simpleCache; ExoPlayer player; @@ -47,7 +53,9 @@ public class VideoPlayer extends AppCompatActivity { playerView = findViewById(R.id.player_view); // creating a variable for exoplayer player = new ExoPlayer.Builder(this).build(); - MediaSource mediaSource = new ProgressiveMediaSource.Factory(new CacheDataSourceFactory(this, 100 * 1024 * 1024, 512 * 1024 * 1024)) + LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize); + simpleCache = new SimpleCache(new File(this.getCacheDir(), "media"), evictor); + MediaSource mediaSource = new ProgressiveMediaSource.Factory(new CacheDataSourceFactory(this, simpleCache, 512 * 1024 * 1024)) .createMediaSource(MediaItem.fromUri(Uri.parse(videoURL))); //MediaSource audioSource = new ProgressiveMediaSource(Uri.parse("url"), // new CacheDataSourceFactory(this, 100 * 1024 * 1024, 5 * 1024 * 1024), new DefaultExtractorsFactory(), null, null); @@ -148,13 +156,14 @@ public class VideoPlayer extends AppCompatActivity { } public void onDestroy() { + simpleCache.release(); player.release(); super.onDestroy(); } - public void onBackPressed() { + /*public void onBackPressed() { player.release(); finish(); - } + }*/ } \ No newline at end of file