Caching videos in ViewVideoActivity.

This commit is contained in:
Alex Ning 2021-06-29 21:27:53 +08:00
parent f638eb1e7e
commit 0f5c2e6f59
2 changed files with 27 additions and 10 deletions

View File

@ -263,10 +263,15 @@ class AppModule {
@Provides @Provides
@Singleton @Singleton
ExoCreator provideExoCreator() { SimpleCache provideSimpleCache() {
SimpleCache cache = new SimpleCache(new File(mApplication.getCacheDir(), "/toro_cache"), return new SimpleCache(new File(mApplication.getCacheDir(), "/exoplayer"),
new LeastRecentlyUsedCacheEvictor(200 * 1024 * 1024), new ExoDatabaseProvider(mApplication)); 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(); .build();
return ToroExo.with(mApplication).getCreator(config); return ToroExo.with(mApplication).getCreator(config);
} }

View File

@ -44,6 +44,8 @@ import com.google.android.exoplayer2.ui.TrackSelectionDialogBuilder;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; 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.exoplayer2.util.Util;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.thefuntasty.hauler.DragDirection; import com.thefuntasty.hauler.DragDirection;
@ -157,6 +159,9 @@ public class ViewVideoActivity extends AppCompatActivity {
@Inject @Inject
Executor mExecutor; Executor mExecutor;
@Inject
SimpleCache mSimpleCache;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -289,8 +294,9 @@ public class ViewVideoActivity extends AppCompatActivity {
loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false); loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false);
} }
} else { } else {
dataSourceFactory = new DefaultDataSourceFactory(ViewVideoActivity.this, dataSourceFactory = new CacheDataSourceFactory(mSimpleCache,
Util.getUserAgent(ViewVideoActivity.this, "Infinity")); new DefaultDataSourceFactory(ViewVideoActivity.this,
Util.getUserAgent(ViewVideoActivity.this, "Infinity")));
player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
preparePlayer(savedInstanceState); preparePlayer(savedInstanceState);
} }
@ -298,7 +304,8 @@ public class ViewVideoActivity extends AppCompatActivity {
videoDownloadUrl = mVideoUri.toString(); videoDownloadUrl = mVideoUri.toString();
videoFileName = FilenameUtils.getName(videoDownloadUrl); videoFileName = FilenameUtils.getName(videoDownloadUrl);
// Produces DataSource instances through which media data is loaded. // 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. // Prepare the player with the source.
player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
preparePlayer(savedInstanceState); preparePlayer(savedInstanceState);
@ -308,7 +315,8 @@ public class ViewVideoActivity extends AppCompatActivity {
id = intent.getStringExtra(EXTRA_ID); id = intent.getStringExtra(EXTRA_ID);
videoFileName = subredditName + "-" + id + ".mp4"; videoFileName = subredditName + "-" + id + ".mp4";
// Produces DataSource instances through which media data is loaded. // 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. // Prepare the player with the source.
player.prepare(new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); player.prepare(new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
preparePlayer(savedInstanceState); preparePlayer(savedInstanceState);
@ -397,8 +405,9 @@ public class ViewVideoActivity extends AppCompatActivity {
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
mVideoUri = Uri.parse(webm); mVideoUri = Uri.parse(webm);
videoDownloadUrl = mp4; videoDownloadUrl = mp4;
dataSourceFactory = new DefaultDataSourceFactory(ViewVideoActivity.this, dataSourceFactory = new CacheDataSourceFactory(mSimpleCache,
Util.getUserAgent(ViewVideoActivity.this, "Infinity")); new DefaultDataSourceFactory(ViewVideoActivity.this,
Util.getUserAgent(ViewVideoActivity.this, "Infinity")));
player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
preparePlayer(savedInstanceState); preparePlayer(savedInstanceState);
} }
@ -472,7 +481,10 @@ public class ViewVideoActivity extends AppCompatActivity {
videoFileName = subredditName + "-" + id + ".mp4"; videoFileName = subredditName + "-" + id + ".mp4";
// Produces DataSource instances through which media data is loaded. // 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. // Prepare the player with the source.
player.prepare(new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri)); player.prepare(new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
preparePlayer(savedInstanceState); preparePlayer(savedInstanceState);