Last active
March 11, 2025 12:25
-
-
Save 423u5/cea631e7b483683bee8c3cbcecb1f997 to your computer and use it in GitHub Desktop.
Exo Player Cache | Progressive Loading | Cache Media Source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.content.Context; | |
| import androidx.annotation.NonNull; | |
| import com.google.android.exoplayer2.MediaItem; | |
| import com.google.android.exoplayer2.source.MediaSource; | |
| import com.google.android.exoplayer2.source.ProgressiveMediaSource; | |
| import com.google.android.exoplayer2.upstream.DataSource; | |
| import com.google.android.exoplayer2.upstream.DefaultDataSource; | |
| import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; | |
| import com.google.android.exoplayer2.upstream.cache.CacheDataSource; | |
| import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor; | |
| import com.google.android.exoplayer2.upstream.cache.SimpleCache; | |
| import java.io.File; | |
| //com.google.android.exoplayer:exoplayer:2.18.6 | |
| //MediaSource source = CacheMediaSource.Builder(context, path).build(); | |
| //exoPlayer.setMediaSource(source); | |
| public class CacheMediaSource implements DataSource.Factory { | |
| private static SimpleCache cache; | |
| private final DefaultDataSource.Factory defaultDataSourceFactory; | |
| private String source = ""; | |
| private CacheMediaSource(Context context) { | |
| buildCache(context); | |
| DefaultHttpDataSource.Factory httpSourceFactory = new DefaultHttpDataSource.Factory(); | |
| defaultDataSourceFactory = new DefaultDataSource.Factory(context, httpSourceFactory); | |
| } | |
| private static synchronized void buildCache(Context context) { | |
| if (cache == null) { | |
| File cacheDir = new File(context.getCacheDir(), /*child*/ "exo-cache"); | |
| LeastRecentlyUsedCacheEvictor evict = new LeastRecentlyUsedCacheEvictor(200 * 1024 * 1024); // for 200Mb | |
| cache = new SimpleCache(cacheDir, evict); | |
| } | |
| } | |
| public static CacheMediaSource Builder(Context context) { | |
| return CacheMediaSource.Builder(context, ""); | |
| } | |
| public static CacheMediaSource Builder(Context context, String source) { | |
| CacheMediaSource exoCache = new CacheMediaSource(context); | |
| exoCache.source = source; | |
| return exoCache; | |
| } | |
| public CacheMediaSource source(String source) { | |
| this.source = source; | |
| return this; | |
| } | |
| public MediaSource build() { | |
| MediaItem item = MediaItem.fromUri(source); | |
| return new ProgressiveMediaSource | |
| .Factory(this) | |
| .createMediaSource(item); | |
| } | |
| @NonNull | |
| @Override | |
| public DataSource createDataSource() { | |
| return new CacheDataSource( | |
| cache, | |
| defaultDataSourceFactory.createDataSource(), | |
| /*flags*/ CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment