com.google.android.exoplayer2.upstream.cache.CacheDataSource Java Examples

The following examples show how to use com.google.android.exoplayer2.upstream.cache.CacheDataSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DownloaderConstructorHelper.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example #2
Source File: DownloaderConstructorHelper.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example #3
Source File: ExoPlayerHelper.java    From ExoPlayer-Wrapper with Apache License 2.0 6 votes vote down vote up
private void enableCache(int maxCacheSizeMb) {
    LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSizeMb * 1024 * 1024);
    File file = new File(mContext.getCacheDir(), "media");
    Log.d("ZAQ", "enableCache (" + maxCacheSizeMb + " MB), file: " + file.getAbsolutePath());
    SimpleCache simpleCache = new SimpleCache(file, evictor);
    mDataSourceFactory = new CacheDataSourceFactory(
            simpleCache,
            mDataSourceFactory,
            new FileDataSourceFactory(),
            new CacheDataSinkFactory(simpleCache, 2 * 1024 * 1024),
            CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR,
            new CacheDataSource.EventListener() {
                @Override
                public void onCacheIgnored(int reason) {
                    Log.d("ZAQ", "onCacheIgnored");
                }

                @Override
                public void onCachedBytesRead(long cacheSizeBytes, long cachedBytesRead) {
                    Log.d("ZAQ", "onCachedBytesRead , cacheSizeBytes: " + cacheSizeBytes + "   cachedBytesRead: " + cachedBytesRead);
                }
            });
}
 
Example #4
Source File: ExoMediaSourceHelper.java    From DKVideoPlayer with Apache License 2.0 5 votes vote down vote up
private DataSource.Factory getCacheDataSourceFactory() {
    if (mCache == null) {
        mCache = newCache();
    }
    return new CacheDataSourceFactory(
            mCache,
            getDataSourceFactory(),
            CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR);
}
 
Example #5
Source File: ExoSourceManager.java    From GSYVideoPlayer with Apache License 2.0 5 votes vote down vote up
/**
 * 获取SourceFactory,是否带Cache
 */
private DataSource.Factory getDataSourceFactoryCache(Context context, boolean cacheEnable, boolean preview, File cacheDir, String uerAgent) {
    if (cacheEnable) {
        Cache cache = getCacheSingleInstance(context, cacheDir);
        if (cache != null) {
            isCached = resolveCacheState(cache, mDataSource);
            return new CacheDataSourceFactory(cache, getDataSourceFactory(context, preview, uerAgent), CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR);
        }
    }
    return getDataSourceFactory(context, preview, uerAgent);
}
 
Example #6
Source File: MediaVideoView.java    From Slide with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #7
Source File: DownloaderConstructorHelper.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
/** Returns a new {@link CacheDataSource} instance. */
public CacheDataSource createCacheDataSource() {
  return onlineCacheDataSourceFactory.createDataSource();
}
 
Example #8
Source File: PlayerService.java    From AndroidAudioExample with MIT License 4 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        @SuppressLint("WrongConstant") 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(this, 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)));
    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();
}
 
Example #9
Source File: DownloaderConstructorHelper.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
/** Returns a new {@link CacheDataSource} instance. */
public CacheDataSource createCacheDataSource() {
  return onlineCacheDataSourceFactory.createDataSource();
}
 
Example #10
Source File: DownloaderConstructorHelper.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
/** Returns a new {@link CacheDataSource} instance. */
public CacheDataSource createCacheDataSource() {
  return onlineCacheDataSourceFactory.createDataSource();
}
 
Example #11
Source File: DownloaderConstructorHelper.java    From MediaSDK with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a new {@link CacheDataSource} instance which accesses cache read-only and throws an
 * exception on cache miss.
 */
public CacheDataSource createOfflineCacheDataSource() {
  return offlineCacheDataSourceFactory.createDataSource();
}
 
Example #12
Source File: DownloaderConstructorHelper.java    From Telegram-FOSS with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns a new {@link CacheDataSource} instance which accesses cache read-only and throws an
 * exception on cache miss.
 */
public CacheDataSource createOfflineCacheDataSource() {
  return offlineCacheDataSourceFactory.createDataSource();
}
 
Example #13
Source File: DownloaderConstructorHelper.java    From Telegram with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns a new {@link CacheDataSource} instance which accesses cache read-only and throws an
 * exception on cache miss.
 */
public CacheDataSource createOfflineCacheDataSource() {
  return offlineCacheDataSourceFactory.createDataSource();
}