google / google/ExoPlayer

Exoplayer not reading from cache

Open
#9,936 5 comments 0 reactions 1 assignee Claimed by @marcbaechinger View on GitHub
question
Dominant language
Java
Stars
21.9k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

In my app, I have been trying to cache the first 100KB of the videos (MP4 format), when the videos are getting downloaded, and then play the video from the cache so that there is no buffering when a user tries to watch the video.

However, though videos are getting cached, as shown in the screenshot below
![image](https://user-images.githubusercontent.com/43119278/152509757-f74a6d9d-8c16-4072-8428-bf34fe48e4c9.png)
Also, if I call, MyApplication.getInstance().getSimpleCache().isCached(playerUri,0,102400), it shows true.

But, If I switch off the internet, and I try to play the video, it shows "unknown host exception, can't connect to the network", which means It is trying to read from the upstream source instead of the cache

Here's the code, for caching, the mp4 file, in the background

```
DataSpec.Builder builder = new DataSpec.Builder();
builder.setUri(url);
builder.setLength(102400);
builder.setKey(url);

DefaultHttpDataSource.Factory defaultDataSourceFactory = new DefaultHttpDataSource.Factory();
CacheDataSource cacheDataSource = new CacheDataSource(MyApplication.getInstance().getSimpleCache(),
defaultDataSourceFactory.createDataSource() );

VideoPreLoadingService videoPreLoadingService = new VideoPreLoadingService();
try {
videoPreLoadingService.cacheVideo(builder.build(), cacheDataSource, new CacheWriter.ProgressListener() {
@Override
public void onProgress(long requestLength, long bytesCached, long newBytesCached) {

if(requestLength == bytesCached){

System.out.println("cached" + bytesCached);
}

}

});
} catch (IOException e) {
e.printStackTrace();

myExecutor.shutdown();

}
```

Here's the videoPreLoadingService.cacheVideo method

```
public void cacheVideo(DataSpec dataSpec, CacheDataSource dataSource, CacheWriter.ProgressListener progressListener) throws IOException {

CacheWriter cacheWriter = new CacheWriter(dataSource, dataSpec, null, progressListener);
cacheWriter.cache();
}
```

Here's code while trying to play from cache, note, I am using a single instance of Exoplayer across the app,

Setting the player, I am using single instance of simple cache, MyApplication.getInstance().getSimpleCache()

```
HttpDataSource.Factory httpDataSourceFactory = new DefaultHttpDataSource.Factory();
cacheDataSourceFactory =
new CacheDataSource.Factory()
.setCache(MyApplication.getInstance().getSimpleCache())
.setUpstreamDataSourceFactory(httpDataSourceFactory)
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)

.setCacheWriteDataSinkFactory(null); // Disable writing.;

/* Instantiate a DefaultLoadControl.Builder. */
DefaultLoadControl.Builder builder = new DefaultLoadControl.Builder();
/* Milliseconds of media data buffered before playback starts or resumes. */
final long loadControlStartBufferMs = 15000;
builder.setBufferDurationsMs(2000,
5000,
1000,
1000);
/* Build the actual DefaultLoadControl instance */
DefaultLoadControl loadControl = builder.build();
player = new ExoPlayer.Builder(context)
.setMediaSourceFactory(new DefaultMediaSourceFactory(cacheDataSourceFactory))
.setLoadControl(loadControl)
.build();
player.setPlayWhenReady(true);
player.addAnalyticsListener(new EventLogger(null));
```

Setting the media source using the same cacheDataSourcefactory

```
MediaItem.Builder builder_ = new MediaItem.Builder();
builder_.setCustomCacheKey(playerUri); //tried both with or without custom key
builder_.setUri(Uri.parse(playerUri));

MediaSource mediaSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory)
.createMediaSource(builder_.build());
player.setMediaSource(mediaSource);
player.prepare();
```

But still it not reading from the cache, I have the documentation, other issues, but couldn't find the solution, please tell me what I am doing wrong.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.