private void configureExoMedia() { // Registers the media sources to use the OkHttp client instead of the standard Apache one // Note: the OkHttpDataSourceFactory can be found in the ExoPlayer extension library `extension-okhttp` ExoMedia.setDataSourceFactoryProvider(new ExoMedia.DataSourceFactoryProvider() { @NonNull @Override public DataSource.Factory provide(@NonNull String userAgent, @Nullable TransferListener<? super DataSource> listener) { // Updates the network data source to use the OKHttp implementation DataSource.Factory upstreamFactory = new OkHttpDataSourceFactory(new OkHttpClient(), userAgent, listener); // Adds a cache around the upstreamFactory Cache cache = new SimpleCache(getCacheDir(), new LeastRecentlyUsedCacheEvictor(50 * 1024 * 1024)); return new CacheDataSourceFactory(cache, upstreamFactory, CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR); } }); }
/** Asserts that the cache contains the given data for {@code uriString}. */ public static void assertDataCached(Cache cache, Uri uri, byte[] expected) throws IOException { CacheDataSource dataSource = new CacheDataSource(cache, DummyDataSource.INSTANCE, 0); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, new DataSpec(uri, DataSpec.FLAG_ALLOW_CACHING_UNKNOWN_LENGTH)); try { inputStream.open(); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } } catch (IOException e) { // Ignore } finally { inputStream.close(); } MoreAsserts.assertEquals("Cached data doesn't match expected for '" + uri + "',", expected, outputStream.toByteArray()); }
/** * 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); } }
@Override public void onCreate() { super.onCreate(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_DEFAULT_CHANNEL_ID, getString(R.string.notification_channel_name), NotificationManagerCompat.IMPORTANCE_DEFAULT); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(notificationChannel); AudioAttributes audioAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .build(); audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setOnAudioFocusChangeListener(audioFocusChangeListener) .setAcceptsDelayedFocusGain(false) .setWillPauseWhenDucked(true) .setAudioAttributes(audioAttributes) .build(); } audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); mediaSession = new MediaSessionCompat(this, "PlayerService"); mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); mediaSession.setCallback(mediaSessionCallback); Context appContext = getApplicationContext(); Intent activityIntent = new Intent(appContext, MainActivity.class); mediaSession.setSessionActivity(PendingIntent.getActivity(appContext, 0, activityIntent, 0)); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null, appContext, MediaButtonReceiver.class); mediaSession.setMediaButtonReceiver(PendingIntent.getBroadcast(appContext, 0, mediaButtonIntent, 0)); exoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), new DefaultTrackSelector(), new DefaultLoadControl()); exoPlayer.addListener(exoPlayerListener); DataSource.Factory httpDataSourceFactory = new OkHttpDataSourceFactory(new OkHttpClient(), Util.getUserAgent(this, getString(R.string.app_name)), null); Cache cache = new SimpleCache(new File(this.getCacheDir().getAbsolutePath() + "/exoplayer"), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 100)); // 100 Mb max this.dataSourceFactory = new CacheDataSourceFactory(cache, httpDataSourceFactory, CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR); this.extractorsFactory = new DefaultExtractorsFactory(); }
@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); }
public OfficeDataSource(Context context, CacheDataSource.EventListener eventListener) { this.context = context; this.eventListener = eventListener; }
@Override public DataSource createDataSource() { return new CacheDataSource(simpleCache, defaultDatasourceFactory.createDataSource(), new FileDataSource(), new CacheDataSink(simpleCache, CacheDataSource.DEFAULT_MAX_CACHE_FILE_SIZE), 0, listener); }
private CacheDataSourceFactory newOfflineCacheDataSourceFactory() { return new CacheDataSourceFactory(cache, DummyDataSource.FACTORY, CacheDataSource.FLAG_BLOCK_ON_CACHE); }
/*** * @param context 上下文 * @param maxCacheSize 缓存大小 * @param secretKey If not null, cache keys will be stored encrypted on filesystem using AES/CBC. The key must be 16 bytes long. * @param listener the listener */ public DefaultCacheDataSourceFactory(@NonNull Context context, long maxCacheSize, byte[] secretKey, @Nullable CacheDataSource.EventListener listener) { defaultDatasourceFactory = new JDefaultDataSourceFactory(context); simpleCache = new SimpleCache(new File(context.getExternalCacheDir(), "media1"), new LeastRecentlyUsedCacheEvictor(maxCacheSize), secretKey); this.listener = listener; }