/** * Returns a new {@link CacheDataSource} instance. If {@code offline} is true, it can only read * data from the cache. */ public CacheDataSource buildCacheDataSource(boolean offline) { DataSource cacheReadDataSource = cacheReadDataSourceFactory != null ? cacheReadDataSourceFactory.createDataSource() : new FileDataSource(); if (offline) { return new CacheDataSource(cache, DummyDataSource.INSTANCE, cacheReadDataSource, null, CacheDataSource.FLAG_BLOCK_ON_CACHE, null); } else { DataSink cacheWriteDataSink = cacheWriteDataSinkFactory != null ? cacheWriteDataSinkFactory.createDataSink() : new CacheDataSink(cache, CacheDataSource.DEFAULT_MAX_CACHE_FILE_SIZE); DataSource upstream = upstreamDataSourceFactory.createDataSource(); upstream = priorityTaskManager == null ? upstream : new PriorityDataSource(upstream, priorityTaskManager, C.PRIORITY_DOWNLOAD); return new CacheDataSource(cache, upstream, cacheReadDataSource, cacheWriteDataSink, CacheDataSource.FLAG_BLOCK_ON_CACHE, null); } }
private static CacheDataSource buildCacheDataSource(Context context, DataSource upstreamSource, boolean useAesEncryption) throws CacheException { File cacheDir = context.getExternalCacheDir(); Cache cache = new SimpleCache(new File(cacheDir, EXO_CACHE_DIR), new NoOpCacheEvictor()); emptyCache(cache); // Source and cipher final String secretKey = "testKey:12345678"; DataSource file = new FileDataSource(); DataSource cacheReadDataSource = useAesEncryption ? new AesCipherDataSource(Util.getUtf8Bytes(secretKey), file) : file; // Sink and cipher CacheDataSink cacheSink = new CacheDataSink(cache, EXO_CACHE_MAX_FILESIZE); byte[] scratch = new byte[3897]; DataSink cacheWriteDataSink = useAesEncryption ? new AesCipherDataSink(Util.getUtf8Bytes(secretKey), cacheSink, scratch) : cacheSink; return new CacheDataSource(cache, upstreamSource, cacheReadDataSource, cacheWriteDataSink, CacheDataSource.FLAG_BLOCK_ON_CACHE, null); // eventListener }
private CacheDataSource createCacheDataSource(boolean setReadException, boolean simulateUnknownLength, @CacheDataSource.Flags int flags, CacheDataSink cacheWriteDataSink) { FakeDataSource upstream = new FakeDataSource(); FakeData fakeData = upstream.getDataSet().newDefaultData() .setSimulateUnknownLength(simulateUnknownLength).appendReadData(TEST_DATA); if (setReadException) { fakeData.appendReadError(new IOException("Shouldn't read from upstream")); } return new CacheDataSource(cache, upstream, new FileDataSource(), cacheWriteDataSink, flags, null); }
@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); }
@Override public DataSource createDataSource() { return new CacheDataSource(simpleCache, defaultDatasourceFactory.createDataSource(), new FileDataSource(), new CacheDataSink(simpleCache, CacheDataSource.DEFAULT_MAX_CACHE_FILE_SIZE), 0, listener); }
private void initPlayer(String url) { // 1.创建一个默认TrackSelector,测量播放过程中的带宽。 如果不需要,可以为null。 DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); //从MediaSource中选出media提供给可用的Render S来渲染,在创建播放器时被注入 TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter); TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); // 2.创建一个默认的LoadControl //Create a default LoadControl 控制MediaSource缓存media DefaultLoadControl loadControl = new DefaultLoadControl(); // BufferingLoadControl bufferingLoadControl = new BufferingLoadControl(); //生成加载媒体数据的DataSource实例。 // dataSourceFactory = new DefaultDataSourceFactory(this, // Util.getUserAgent(this, "aitrip"), bandwidthMeter); //自定义解密工厂 aitripFactory = new AitripDataSourceFactory(this, Util.getUserAgent(this, "aitrip"), bandwidthMeter); extractorsFactory = new DefaultExtractorsFactory(); // factory = new DiyExtractorsFactory(); FileDataSource fileDataSource = new FileDataSource(); //test mp3 // url = "https://storage.googleapis.com/exoplayer-test-media-0/play.mp3"; // MediaSource代表要播放的媒体。 videoSource = new ExtractorMediaSource(Uri.parse(url), aitripFactory, extractorsFactory, null, null); // 3.创建播放器 player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl); //SimpleExoPlayerView simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.exo_player); //设置监听器 player.addListener(eventListener); //装载多个资源 MediaSource[] mediaSources = new MediaSource[1]; // mediaSources[0] = buildMediaSource(Uri.parse(huai), ""); // mediaSources[1] = buildMediaSource(Uri.parse(deng), ""); mediaSources[0] = buildMediaSource(Uri.parse(deng), ""); // mediaSources[2] = buildMediaSource(playerUri, ""); // mediaSources[3] = buildMediaSource(Uri.parse(deng), ""); ConcatenatingMediaSource mediaSource = new ConcatenatingMediaSource(mediaSources); //设置资源 player.prepare(mediaSource); window = new Timeline.Window(); }
/** * Constructs an instance with default {@link DataSource} and {@link DataSink} instances for * reading and writing the cache. The sink is configured to fragment data such that no single * cache file is greater than maxCacheFileSize bytes. * * @param cache The cache. * @param upstream A {@link DataSource} for reading data not in the cache. * @param flags A combination of {@link #FLAG_BLOCK_ON_CACHE}, {@link #FLAG_IGNORE_CACHE_ON_ERROR} * and {@link #FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS}, or 0. * @param maxCacheFileSize The maximum size of a cache file, in bytes. If the cached data size * exceeds this value, then the data will be fragmented into multiple cache files. The * finer-grained this is the finer-grained the eviction policy can be. */ public CacheDataSource(Cache cache, DataSource upstream, @Flags int flags, long maxCacheFileSize) { this(cache, upstream, new FileDataSource(), new CacheDataSink(cache, maxCacheFileSize), flags, null); }
/** * Constructs an instance with default {@link DataSource} and {@link DataSink} instances for * reading and writing the cache. The sink is configured to fragment data such that no single * cache file is greater than maxCacheFileSize bytes. * * @param cache The cache. * @param upstream A {@link DataSource} for reading data not in the cache. * @param flags A combination of {@link #FLAG_BLOCK_ON_CACHE} and {@link * #FLAG_IGNORE_CACHE_ON_ERROR} or 0. * @param maxCacheFileSize The maximum size of a cache file, in bytes. If the cached data size * exceeds this value, then the data will be fragmented into multiple cache files. The * finer-grained this is the finer-grained the eviction policy can be. */ public CacheDataSource(Cache cache, DataSource upstream, @Flags int flags, long maxCacheFileSize) { this(cache, upstream, new FileDataSource(), new CacheDataSink(cache, maxCacheFileSize), flags, null); }
/** * Constructs an instance with default {@link DataSource} and {@link DataSink} instances for * reading and writing the cache. The sink is configured to fragment data such that no single * cache file is greater than maxCacheFileSize bytes. * * @param cache The cache. * @param upstream A {@link DataSource} for reading data not in the cache. * @param flags A combination of {@link #FLAG_BLOCK_ON_CACHE}, {@link #FLAG_IGNORE_CACHE_ON_ERROR} * and {@link #FLAG_CACHE_UNBOUNDED_REQUESTS} or 0. * @param maxCacheFileSize The maximum size of a cache file, in bytes. If the cached data size * exceeds this value, then the data will be fragmented into multiple cache files. The * finer-grained this is the finer-grained the eviction policy can be. */ public CacheDataSource(Cache cache, DataSource upstream, @Flags int flags, long maxCacheFileSize) { this(cache, upstream, new FileDataSource(), new CacheDataSink(cache, maxCacheFileSize), flags, null); }