I'm traying to use ExoPlayer for playback video over http. And I want to save video after video was loaded and play it from cache. How Do implement cache and playback from cache? Can give me any samples.
- 
                    I don't think there is default behavior that you can do this easily. It is open source. You can fork it and change the download location of the cache. – tasomaniac Feb 04 '15 at 11:10
 - 
                    I have a same problem. do you find way for implementing this? – Siavash Abdoli Sep 28 '15 at 07:42
 - 
                    @fisher3421 have you got any solution I am also looking for same. – Sagar Feb 22 '18 at 06:57
 
4 Answers
You use cacheDataSource created using cache and dataSource. This cacheDataSource is then used by ExtractorSampleSource.Below is the code for audioRenderer, similarly can be done for videoRender; passing to exoplayerInstance.prepare(renderers).
Cache cache = new SimpleCache(mCtx.getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 10));
DataSource dataSource = new DefaultUriDataSource(mCtx, "My Player");
CacheDataSource cacheDataSource = new CacheDataSource(cache, dataSource, false, false);
Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
ExtractorSampleSource extractorSampleSource = new ExtractorSampleSource(trackURI, cacheDataSource, allocator, BUFFER_SEGMENT_COUNT*BUFFER_SEGMENT_SIZE, new Mp3Extractor());
MediaCodecAudioTrackRenderer audioTrackRenderer = new MediaCodecAudioTrackRenderer(extractorSampleSource);
- 304
 - 4
 - 7
 
- 
                    is this the entire implementation for getting caching to work? I've implemented this, but printing `cache.getCacheSpace()` shows 0 no matter what I do. Any ideas or tips to filling the cache? – welshk91 Oct 29 '15 at 17:42
 
What protocol are you using mpeg-dash or plain http.
You can override HttpDataSource and write incoming bytes to a file and when playing again check if file exists at the desired location and change the InputStream fed to the player from your file instead of HttpDataSource.
- 3,965
 - 2
 - 17
 - 18
 
- 
                    2Overriding HttpDataSource is wrong approach because ExoPlayer has target classes for cache implementation. For example CacheDataSource. I would like get right way of using this classes. – vmtrue Mar 30 '15 at 09:09
 - 
                    I wrote datasource for this purpose but downloaded file is some KB's bigger that expected. I don't know why. – David Jan 14 '19 at 10:26
 
I use exoplayer with this library: https://github.com/danikula/AndroidVideoCache It helps you cache the video from a resource (URL, file)...
This is my sample code:
String mediaURL = "https://my_cool_vid.com/vi.mp4";
SimpleExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(getContext());
HttpProxyCacheServer proxyServer = HttpProxyCacheServer.Builder(getContext()).maxCacheSize(1024 * 1024 * 1024).build();
String proxyURL = proxyServer.getProxyUrl(mediaURL);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getContext(),
                Util.getUserAgent(getContext(), getActivity().getApplicationContext().getPackageName()));
exoPlayer.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory)
                .createMediaSource(Uri.parse(proxyURL)););
- 6,198
 - 3
 - 24
 - 20
 
This library is easy to use: https://github.com/danikula/AndroidVideoCache. You just need to have the initialization code found in the repo in an appcontroller.
For those using mediaitem, this is what you can do:
exoPlayer = new SimpleExoPlayer.Builder(context).build();
    holder.exoPlayerView.setPlayer(exoPlayer);
    HttpProxyCacheServer proxyServer = AppController.getProxy(context);
    String streamingURL = shortVideosRecommendationsArrayList.get(holder.getAbsoluteAdapterPosition()).getStreamingURL();
    String proxyURL = proxyServer.getProxyUrl(streamingURL);
  
    MediaItem mediaItem = MediaItem.fromUri(proxyURL);
    exoPlayer.setMediaItem(mediaItem);
    exoPlayer.setRepeatMode(exoPlayer.REPEAT_MODE_ALL);
    exoPlayer.prepare();
    exoPlayer.setPlayWhenReady(true);
- 144
 - 2
 - 7