cache release
This commit is contained in:
parent
1ca6c42e87
commit
71dcf59650
2 changed files with 19 additions and 8 deletions
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}*/
|
||||
}
|
Loading…
Reference in a new issue