在ExoPlayer中使用caching

我正在寻找在ExoPlayer中实现caching的任何示例。

ExoPlayer在其库中有不同的caching类,Google在本video中解释说,我们可以使用CacheDataSource类来实现它,但Google不提供任何演示。 不幸的是,这似乎使用起来相当复杂,所以我现在正在寻找例子(在谷歌上没有成功)。

有没有人成功或有任何信息会帮助? 谢谢。

Solutions Collecting From Web of "在ExoPlayer中使用caching"

这是ExoPlayer 2的解决scheme

创build一个自定义caching数据源工厂

class CacheDataSourceFactory implements DataSource.Factory { private final Context context; private final DefaultDataSourceFactory defaultDatasourceFactory; private final long maxFileSize, maxCacheSize; 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)); } @Override public DataSource createDataSource() { LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize); SimpleCache simpleCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor); return new CacheDataSource(simpleCache, defaultDatasourceFactory.createDataSource(), new FileDataSource(), new CacheDataSink(simpleCache, maxFileSize), CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null); } } 

和玩家

 BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter); TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); SimpleExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector); MediaSource audioSource = new ExtractorMediaSource(Uri.parse(url), new CacheDataSourceFactory(context, 100 * 1024 * 1024, 5 * 1024 * 1024), new DefaultExtractorsFactory(), null, null); exoPlayer.setPlayWhenReady(true); exoPlayer.prepare(audioSource); 

它工作得很好。

这是一个用OkHttpreplace演示数据源的例子,默认是没有cachinghttps://github.com/b95505017/ExoPlayer/commit/ebfdda8e7848a2e2e275f5c0525f614b56ef43a6 https://github.com/b95505017/ExoPlayer/tree/okhttp_http_data_source所以,你只是需要正确configurationOkHttpcaching,请求应该被caching。

我已经在渲染器构build器中像这样实现了它

 private static final int BUFFER_SEGMENT_SIZE = 64 * 1024; private static final int BUFFER_SEGMENT_COUNT = 160; final String userAgent = Util.getUserAgent(mContext, appName); final DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); final Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);* Cache cache = new SimpleCache(context.getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 10)); DataSource dataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent); CacheDataSource cacheDataSource = new CacheDataSource(cache, dataSource, false, false); ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri , cacheDataSource , allocator , BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE , new Mp4Extractor());