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

The following examples show how to use com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory. 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: 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 #2
Source File: ExoPlayerView.java    From AerialDream with GNU General Public License v3.0 6 votes vote down vote up
public void setUri(Uri uri) {
    if (uri == null) {
        return;
    }

    player.stop();
    prepared = false;

    DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory("Aerial Dream");
    DataSource.Factory dataSourceFactory = cacheSize > 0
            ? new CacheDataSourceFactory(new SimpleCache(getContext().getCacheDir(), new LeastRecentlyUsedCacheEvictor(cacheSize), new ExoDatabaseProvider(getContext())), httpDataSourceFactory, 0)
            : httpDataSourceFactory;

    mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(uri);
    player.prepare(mediaSource);
}
 
Example #3
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 #4
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 #5
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();
}