I have searched for cache in/of ExoPlayer
but i neither understoond them nor could implement them correctly.
In this asked question for using cache in Exoplayer there are some answers with sample code. The first answer that i’m not able to implement it is in part two of sample code which said to use the class DefaultHttpDataSourceFactory
that i suppose it was changed with the class DefaultHttpDataSource.Factory()
that does not seem much of a problem but the main problem i faced was this line of code
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
SimpleExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
MediaSource audioSource = new ExtractorMediaSource(Uri.parse(url),
new CacheDataSourceFactory(context, 100 * 1024 * 1024, 5 * 1024 * 1024), new DefaultExtractorsFactory(), null, null);
exoPlayer.setPlayWhenReady(true);
exoPlayer.prepare(audioSource);
that says use class ExtractorMediaSource
which there is not calss by this name, i even did not find in depricated
classes of exoplayer document. Almost evetything has changed in exoplayer in version 2.18.0
. For instance, the part that we should set MediaSource
for exoplayer now it is sepreated into two parts:
final DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, "agent name"); //part one
final ProgressiveMediaSource.Factory mediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory); //part two
I hope you have seen the differences and my problem with this solution
And now next solution and its problem: This dear friend suggest to use This Library which sound good and some how solved my problem but what this dependency compile 'com.danikula:videocache:2.7.1'
actually does is to download the video and save it in app folder. since android Pie(9)
and new privacy plicy of google and android phone producers, they do not allow to access app folder.
And the same issue with other solutions that are in Kotlin, the classes in exoplayer are missing.
What i want:
I want to use cache in exoplayer2(or cache of exoplayer2) like Glide
that save image in app cache i want to implement similar functionality for my exoplayer2, i have searched almost everywhere.
Note:
I use these dependecies of ExoPlayer
:
implementation 'com.google.android.exoplayer:exoplayer-core:2.18.0'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.18.0'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.18.0'
implementation 'com.google.android.exoplayer:exoplayer:2.18.0'
I am finished with my app just need to figure out this part of it.
2
Answers
Use Builder class to create player object and set MediaSourceFactory to caching like this:
and then everywhere you pass media source to exoplayer: