ExoPlayer icon indicating copy to clipboard operation
ExoPlayer copied to clipboard

Save Offline Downloaded Content in SD Card

Open piyush2dev opened this issue 6 years ago • 5 comments

We have implemented Offline DRM using Exoplayer. Currently videos .exo gets downloaded in Internal Storage.

How to store .exo files to sd card instead of Phone Memory.

Will this method return the Simple Cache of .exo files in SD card:

 /** Returns a {@link DataSource.Factory}. */
  public DataSource.Factory buildDataSourceFactory() {
    DefaultDataSourceFactory upstreamFactory =
        new DefaultDataSourceFactory(this, buildHttpDataSourceFactory());
    return buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache());
  }

piyush2dev avatar Mar 19 '20 10:03 piyush2dev

I think writing to an sdcard only involves using a different path pointing to that directory on the sd card. In case you know the path to that directory you probably just need to have a Cache implementation which reads/writes from this location.

If you use such a Cache pointing to a sdcard for downloading, you can create a CacheDataSource with the same Cache for reading also:

new CacheDataSourceFactory(
        cache,
        upstreamFactory,
        new FileDataSource.Factory(),
        /* cacheWriteDataSinkFactory= */ null,
        CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR,
        /* eventListener= */ null);

Did you experience any problems when using a path pointing to the sdcard?

marcbaechinger avatar Mar 19 '20 12:03 marcbaechinger

Thanks @marcbaechinger Based on your input I was able to see that i can control over cache. File downloadContentDirectory = new File(getDownloadDirectory(), DOWNLOAD_CONTENT_DIRECTORY);

The thing is that I want to give user an option whether user wants to download to SD CARD OR INTERNAL MEMORY.

Do I need to create 2 DownloadManager Object here. 1 pointing to SD card and 1 pointing to Internal Memory.

piyush2dev avatar Mar 19 '20 13:03 piyush2dev

@marcbaechinger @erdemguven @willnorris Can u please help in above query.

The thing is that I want to give user an option whether user wants to download to SD CARD OR INTERNAL MEMORY.

Do I need to create 2 DownloadManager Object here. 1 pointing to SD card and 1 pointing to Internal Memory.

Also at time of retrieval for getting SimpleCache to play content. Do I need create 2 objects for buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache())

As DownManager uses getDownloadCache()

piyush2dev avatar Mar 19 '20 13:03 piyush2dev

You only need one DownloadManager pointing to one Cache. If you do playback you want to create a CacheDataSource to get the data from the cache, if available. This is the same cache as the DownloadManager uses.

This Cache points to a directory you can choose and you can let the user decide where that is. Be aware that it might be tricky to change that directory afterwards without loosing downloads.

You also need to ask for the permission to access external storage if your user chooses to store on an sd card.

marcbaechinger avatar Mar 19 '20 17:03 marcbaechinger

Closing due to inactivity. Please reopen if required.

marcbaechinger avatar Apr 15 '20 08:04 marcbaechinger