ExoPlayer cache
Asked Answered
I

4

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.

Insoluble answered 29/9, 2014 at 8:8 Comment(3)
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.Judithjuditha
I have a same problem. do you find way for implementing this?Paramo
@fisher3421 have you got any solution I am also looking for same.Tights
S
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);
Steenbok answered 26/10, 2015 at 12:18 Comment(1)
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?Backset
S
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.

Superpatriot answered 29/3, 2015 at 10:5 Comment(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.Insoluble
I wrote datasource for this purpose but downloaded file is some KB's bigger that expected. I don't know why.Natasha
C
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)););
Cufic answered 3/11, 2019 at 7:23 Comment(0)
H
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);
Herzl answered 19/9, 2021 at 19:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.