18

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.

vmtrue
  • 1,724
  • 18
  • 36

4 Answers4

5

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);
Saksham
  • 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
2

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.

Dheeraj Sachan
  • 3,965
  • 2
  • 17
  • 18
  • 2
    Overriding 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
1

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)););
Dũng Trần Trung
  • 6,198
  • 3
  • 24
  • 20
1

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);
Ziyaad Shiraz
  • 144
  • 2
  • 7