Uso del caché en ExoPlayer
Estoy buscando cualquier ejemplo de implementación de caché en ExoPlayer.
ExoPlayer tiene en su biblioteca diferentes clases sobre caché y Google explica en este video que podemos implementarlo con la clase CacheDataSource, pero Google no proporciona ninguna demo en él. Desafortunadamente esto parece bastante complicado de usar, por lo que estoy buscando ejemplos (sin éxito en Google).
- Mejor manera de combinar archivos de audio en Android
- ¿Cómo administrar el estado de Android MediaPlayer, errores y excepciones?
- Archivo de vídeo sdk cut / trim de Android
- Tema personalizado para Media Volume Controller (lo que se llame) de Android?
- Hacer un bucle de audio en Android
¿Cualquier persona tiene éxito o tiene cualquier Info que ayudaría? Gracias.
- Cómo grabar vídeo desde el fondo de la aplicación: Android
- Cómo grabar un audio FM en android?
- Android MediaPlayer seekTo no funciona
- Ruta de audio a través del altavoz cuando los auriculares lo conectan
- Cómo utilizar ByteBuffer en el contexto de MediaCodec en android
- Android MediaMetadataRetriever devuelve valores nulos de la mayoría de las claves
- Cómo obtener una lista de reproductores multimedia instalados
- Cómo obtener canciones del álbum / artista en android, usando MediametadataRetriever?
Aquí hay un ejemplo que reemplaza la fuente de datos de demostración con OkHttp, por defecto no hay caché https://github.com/b95505017/ExoPlayer/commit/ebfdda8e7848a2e2e275f5c0525f614b56ef43a6 https://github.com/b95505017/ExoPlayer/tree/okhttp_http_data_source Necesidad de configurar caché de OkHttp correctamente y las peticiones se deben almacenar en caché.
Lo he implementado de esta manera en el generador de renderizado
private static final int BUFFER_SEGMENT_SIZE = 64 * 1024; private static final int BUFFER_SEGMENT_COUNT = 160; final String userAgent = Util.getUserAgent(mContext, appName); final DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); final Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);* Cache cache = new SimpleCache(context.getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 10)); DataSource dataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent); CacheDataSource cacheDataSource = new CacheDataSource(cache, dataSource, false, false); ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri , cacheDataSource , allocator , BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE , new Mp4Extractor());
Aquí está la solución para ExoPlayer 2. +
Cree una fábrica de origen de datos de caché personalizada
class CacheDataSourceFactory implements DataSource.Factory { private final Context context; private final DefaultDataSourceFactory defaultDatasourceFactory; private final long maxFileSize, maxCacheSize; CacheDataSourceFactory(Context context, long maxCacheSize, long maxFileSize) { super(); this.context = context; this.maxCacheSize = maxCacheSize; this.maxFileSize = maxFileSize; String userAgent = Util.getUserAgent(context, context.getString(R.string.app_name)); DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); defaultDatasourceFactory = new DefaultDataSourceFactory(this.context, bandwidthMeter, new DefaultHttpDataSourceFactory(userAgent, bandwidthMeter)); } @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); } }
Y el jugador
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter); TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); SimpleExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector); MediaSource audioSource = new ExtractorMediaSource(Uri.parse(url), new CacheDataSourceFactory(context, 100 * 1024 * 1024, 5 * 1024 * 1024), new DefaultExtractorsFactory(), null, null); exoPlayer.setPlayWhenReady(true); exoPlayer.prepare(audioSource);
Funciona bastante bien.