diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-06-29 13:27:53 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-06-29 13:27:53 +0000 |
commit | 0f5c2e6f598145705b2a2c9bf44520eee1766b49 (patch) | |
tree | 72c64704e4c0dd1a3893bb8f4ff74b9985a93944 /app/src/main/java | |
parent | f638eb1e7eada7098b2335fbcd81b106f6a304f0 (diff) | |
download | infinity-for-reddit-0f5c2e6f598145705b2a2c9bf44520eee1766b49.tar infinity-for-reddit-0f5c2e6f598145705b2a2c9bf44520eee1766b49.tar.gz infinity-for-reddit-0f5c2e6f598145705b2a2c9bf44520eee1766b49.tar.bz2 infinity-for-reddit-0f5c2e6f598145705b2a2c9bf44520eee1766b49.tar.lz infinity-for-reddit-0f5c2e6f598145705b2a2c9bf44520eee1766b49.tar.xz infinity-for-reddit-0f5c2e6f598145705b2a2c9bf44520eee1766b49.tar.zst infinity-for-reddit-0f5c2e6f598145705b2a2c9bf44520eee1766b49.zip |
Caching videos in ViewVideoActivity.
Diffstat (limited to '')
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java | 11 | ||||
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java | 26 |
2 files changed, 27 insertions, 10 deletions
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); |