ExoPlayer 下载后从缓存/离线播放文件
ExoPlayer play the file from cache / offline after downloading
我正在使用 ExoPlayer 并且我有一个文件必须在从顶部再次完成电影播放后自动播放。
我使用了 LoopingMediaSource,但每次电影从头开始时都会出现问题,它会再次开始下载文件,但我希望在电影第一次离线显示后
这是我的自定义 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);
}
}
这是我的 exo 播放器代码
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);
下载完成后,您需要将此 url 及其下载状态保存在您的移动数据库中
//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 }
我正在使用 ExoPlayer 并且我有一个文件必须在从顶部再次完成电影播放后自动播放。 我使用了 LoopingMediaSource,但每次电影从头开始时都会出现问题,它会再次开始下载文件,但我希望在电影第一次离线显示后 这是我的自定义 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);
}
}
这是我的 exo 播放器代码
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);
下载完成后,您需要将此 url 及其下载状态保存在您的移动数据库中
//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 }