I'm using ExoPlayer and I have a file which must auto play after finishing movie play from top again. I used LoopingMediaSource but here is the problem every time that movie start from the beginning it start download the file again but I expect after the first time movie display offline here is my custom CacheDataSourceFactory class
public class CacheDataSourceFactory implements DataSource.Factory {
private final Context context;
private final DefaultDataSourceFactory defaultDatasourceFactory;
private final long maxFileSize, maxCacheSize;
public static CacheDataSourceFactory cacheDataSourceFactory;
SimpleCache simpleCache;
public static CacheDataSourceFactory getInstance(Context context, long maxCacheSize, long maxFileSize) {
if (cacheDataSourceFactory==null) {
cacheDataSourceFactory = new CacheDataSourceFactory(context , maxCacheSize , maxFileSize);
}
return cacheDataSourceFactory;
}
private CacheDataSourceFactory(Context context, long maxCacheSize, long maxFileSize) {
super();
this.context = context;
this.maxCacheSize = maxCacheSize;
this.maxFileSize = maxFileSize;
String userAgent = Util.getUserAgent(context, context.getString(R.string.app_name));
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
defaultDatasourceFactory = new DefaultDataSourceFactory(this.context,
bandwidthMeter,
new DefaultHttpDataSourceFactory(userAgent, bandwidthMeter));
LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize);
simpleCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor);
}
@Override
public DataSource createDataSource() {
return new CacheDataSource(simpleCache, defaultDatasourceFactory.createDataSource(),
new FileDataSource(), new CacheDataSink(simpleCache, maxFileSize),
CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null);
}
}
and here is my exo player code
defaultBandwidthMeter = new DefaultBandwidthMeter();
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl();
player = ExoPlayerFactory.newSimpleInstance(context, trackSelector, loadControl);
DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
dataSourceFactory = new DefaultDataSourceFactory(context,
Util.getUserAgent(context, context.getResources().getString(R.string.app_name)), defaultBandwidthMeter);
MediaSource mediaSource =
new ExtractorMediaSource(
Uri.parse(banner.getImagePath()),
CacheDataSourceFactory.getInstance(context, 100 * 1024 * 1024, 10 * 1024 * 1024), extractorsFactory, null, null);
holder.player_view.hideController();
LoopingMediaSource loopingSource = new LoopingMediaSource(mediaSource);
player.prepare(loopingSource, false, false);
player.setPlayWhenReady(true);
Once the download is complete you need to save this url and it's download state in your mobile db
//if(check already downloaded then execute this
dataSourceFactory = new CacheDataSourceFactory(simpleCache ,
DefaultHttpDataSourceFactory("test"));
mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(yourUri));
player.prepare(mediaSource);
else {
// download /play }