android - ExoPlayer 下载后从缓存/离线播放文件
问题描述
我正在使用 ExoPlayer,并且我有一个文件必须在再次从顶部完成电影播放后自动播放。我使用 LoopingMediaSource 但每次电影从头开始时都会出现问题,它会再次开始下载文件,但我希望在第一次离线电影显示后这是我的自定义 CacheDataSourceFactory 类
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);
解决方案
下载完成后,您需要保存此网址及其在您的移动数据库中的下载状态
//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 }
推荐阅读
- javascript - 为什么我不能将 AdjecentHTML 插入文本区域?
- javascript - 如何在JS中以隐藏模式运行exe?
- python-3.x - 单元测试中的 AssertionError
- sql - Conversion issue from string to decimal in redshift
- php - Problem with joining three tables in Laravel
- c# - 在 SignalR Core 的 Hub 之外访问或注入 HubCallerContext
- android - How to set major and minor grid lines using Androidplot?
- animation - 如何使用 Three.js 动画系统为相机设置动画?
- android - 安卓。是否有进入默认应用页面的意图?
- json - 如何引用文件中的单个元素?