cache release

This commit is contained in:
Page Asgardius 2022-10-04 18:45:54 -07:00
parent 1ca6c42e87
commit 71dcf59650
2 changed files with 19 additions and 8 deletions

View file

@ -17,13 +17,15 @@ import java.io.File;
public class CacheDataSourceFactory implements DataSource.Factory { public class CacheDataSourceFactory implements DataSource.Factory {
private final Context context; private final Context context;
private final DefaultDataSource.Factory defaultDatasourceFactory; private final DefaultDataSource.Factory defaultDatasourceFactory;
private final long maxFileSize, maxCacheSize; private final long maxFileSize;
SimpleCache simpleCache;
//DatabaseProvider databaseProvider = ExoDatabaseProvider(this); //DatabaseProvider databaseProvider = ExoDatabaseProvider(this);
public CacheDataSourceFactory(Context context, long maxCacheSize, long maxFileSize) { public CacheDataSourceFactory(Context context, SimpleCache simpleCache, long maxFileSize) {
super(); super();
this.context = context; this.context = context;
this.maxCacheSize = maxCacheSize; this.simpleCache = simpleCache;
//this.maxCacheSize = maxCacheSize;
this.maxFileSize = maxFileSize; this.maxFileSize = maxFileSize;
String userAgent = Util.getUserAgent(context, context.getString(R.string.app_name)); String userAgent = Util.getUserAgent(context, context.getString(R.string.app_name));
//DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); //DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
@ -32,8 +34,8 @@ public class CacheDataSourceFactory implements DataSource.Factory {
@Override @Override
public DataSource createDataSource() { public DataSource createDataSource() {
LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize); //LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize);
SimpleCache simpleCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor); //simpleCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor);
return new CacheDataSource(simpleCache, defaultDatasourceFactory.createDataSource(), return new CacheDataSource(simpleCache, defaultDatasourceFactory.createDataSource(),
new FileDataSource(), new CacheDataSink(simpleCache, maxFileSize), new FileDataSource(), new CacheDataSink(simpleCache, maxFileSize),
CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null); CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null);

View file

@ -19,6 +19,10 @@ import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource; import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.ui.StyledPlayerView; import com.google.android.exoplayer2.ui.StyledPlayerView;
import com.google.android.exoplayer2.upstream.HttpDataSource; 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. * 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 WifiManager.WifiLock mWifiLock;
private PowerManager.WakeLock mWakeLock; private PowerManager.WakeLock mWakeLock;
private PowerManager powerManager; private PowerManager powerManager;
private long maxCacheSize = 100 * 1024 * 1024;
SimpleCache simpleCache;
ExoPlayer player; ExoPlayer player;
@ -47,7 +53,9 @@ public class VideoPlayer extends AppCompatActivity {
playerView = findViewById(R.id.player_view); playerView = findViewById(R.id.player_view);
// creating a variable for exoplayer // creating a variable for exoplayer
player = new ExoPlayer.Builder(this).build(); 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))); .createMediaSource(MediaItem.fromUri(Uri.parse(videoURL)));
//MediaSource audioSource = new ProgressiveMediaSource(Uri.parse("url"), //MediaSource audioSource = new ProgressiveMediaSource(Uri.parse("url"),
// new CacheDataSourceFactory(this, 100 * 1024 * 1024, 5 * 1024 * 1024), new DefaultExtractorsFactory(), null, null); // 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() { public void onDestroy() {
simpleCache.release();
player.release(); player.release();
super.onDestroy(); super.onDestroy();
} }
public void onBackPressed() { /*public void onBackPressed() {
player.release(); player.release();
finish(); finish();
} }*/
} }