From 0f5c2e6f598145705b2a2c9bf44520eee1766b49 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 29 Jun 2021 21:27:53 +0800 Subject: Caching videos in ViewVideoActivity. --- .../infinityforreddit/AppModule.java | 11 ++++++--- .../activities/ViewVideoActivity.java | 26 ++++++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index 845f1b49..18e43156 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -263,10 +263,15 @@ class AppModule { @Provides @Singleton - ExoCreator provideExoCreator() { - SimpleCache cache = new SimpleCache(new File(mApplication.getCacheDir(), "/toro_cache"), + SimpleCache provideSimpleCache() { + return new SimpleCache(new File(mApplication.getCacheDir(), "/exoplayer"), new LeastRecentlyUsedCacheEvictor(200 * 1024 * 1024), new ExoDatabaseProvider(mApplication)); - Config config = new Config.Builder(mApplication).setMediaSourceBuilder(MediaSourceBuilder.LOOPING).setCache(cache) + } + + @Provides + @Singleton + ExoCreator provideExoCreator(SimpleCache simpleCache) { + Config config = new Config.Builder(mApplication).setMediaSourceBuilder(MediaSourceBuilder.LOOPING).setCache(simpleCache) .build(); return ToroExo.with(mApplication).getCreator(config); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java index c18fcc89..849a3853 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java @@ -44,6 +44,8 @@ import com.google.android.exoplayer2.ui.TrackSelectionDialogBuilder; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; +import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory; +import com.google.android.exoplayer2.upstream.cache.SimpleCache; import com.google.android.exoplayer2.util.Util; import com.google.android.material.snackbar.Snackbar; import com.thefuntasty.hauler.DragDirection; @@ -157,6 +159,9 @@ public class ViewVideoActivity extends AppCompatActivity { @Inject Executor mExecutor; + @Inject + SimpleCache mSimpleCache; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -289,8 +294,9 @@ public class ViewVideoActivity extends AppCompatActivity { loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false); } } else { - dataSourceFactory = new DefaultDataSourceFactory(ViewVideoActivity.this, - Util.getUserAgent(ViewVideoActivity.this, "Infinity")); + dataSourceFactory = new CacheDataSourceFactory(mSimpleCache, + new DefaultDataSourceFactory(ViewVideoActivity.this, + Util.getUserAgent(ViewVideoActivity.this, "Infinity"))); player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); preparePlayer(savedInstanceState); } @@ -298,7 +304,8 @@ public class ViewVideoActivity extends AppCompatActivity { videoDownloadUrl = mVideoUri.toString(); videoFileName = FilenameUtils.getName(videoDownloadUrl); // Produces DataSource instances through which media data is loaded. - dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(this, "Infinity")); + dataSourceFactory = new CacheDataSourceFactory(mSimpleCache, + new DefaultHttpDataSourceFactory(Util.getUserAgent(this, "Infinity"))); // Prepare the player with the source. player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); preparePlayer(savedInstanceState); @@ -308,7 +315,8 @@ public class ViewVideoActivity extends AppCompatActivity { id = intent.getStringExtra(EXTRA_ID); videoFileName = subredditName + "-" + id + ".mp4"; // Produces DataSource instances through which media data is loaded. - dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(this, "Infinity")); + dataSourceFactory = new CacheDataSourceFactory(mSimpleCache, + new DefaultHttpDataSourceFactory(Util.getUserAgent(this, "Infinity"))); // Prepare the player with the source. player.prepare(new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); preparePlayer(savedInstanceState); @@ -397,8 +405,9 @@ public class ViewVideoActivity extends AppCompatActivity { progressBar.setVisibility(View.GONE); mVideoUri = Uri.parse(webm); videoDownloadUrl = mp4; - dataSourceFactory = new DefaultDataSourceFactory(ViewVideoActivity.this, - Util.getUserAgent(ViewVideoActivity.this, "Infinity")); + dataSourceFactory = new CacheDataSourceFactory(mSimpleCache, + new DefaultDataSourceFactory(ViewVideoActivity.this, + Util.getUserAgent(ViewVideoActivity.this, "Infinity"))); player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); preparePlayer(savedInstanceState); } @@ -472,7 +481,10 @@ public class ViewVideoActivity extends AppCompatActivity { videoFileName = subredditName + "-" + id + ".mp4"; // Produces DataSource instances through which media data is loaded. - dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(ViewVideoActivity.this, "Infinity")); + dataSourceFactory = new CacheDataSourceFactory(mSimpleCache, + new DefaultHttpDataSourceFactory( + Util.getUserAgent(ViewVideoActivity.this, + "Infinity"))); // Prepare the player with the source. player.prepare(new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); preparePlayer(savedInstanceState); -- cgit v1.2.3