Lets Dive into Exo-Player (Part IV): Caching Video

Streaming is the continuous transmission of audio or video files from a server to a client (the process that makes watching videos online possible)
What is Buffering?

Buffering is something that happens while playing a video. It is the practice of pre-loading chunks of video content & it helps to make streaming run more smoothly because videos can start playing before the entire video is loaded.
Reason for too much video buffer?
Now there can be a list of reasons that can cause too much buffering some of them are Slow Internet, Streaming HD Content, and Too much latency in the Network.

What's the solution then 🤔 ?
Now since too much, buffering can be a major pain point for any of our app users we have many different solutions to this.

Now in this article, we are going to cover how can one Cache the video content
Implementation
Basic Setup
For the basic setup, you can follow Part 1 :
Demo Repo
Now the internal caching work along with the Data Source Factory that is used by our ExoPlayer.
Here, Data Source Factory is a Data Stream Reader that reads the chunk of a stream over the network or local or cache source and makes it available to ExoPlayer.
To Cache our Data Source we will be using Cache Data Source that will use the basic android cache along with it there is a certain configuration we can add with it.
Here is how :
Now here we are simply using the basic cache along with a external file directory i.e Download Directory
StandaloneDatabaseProvider is a database provider that is used by our Media Player(ExoPlayer) which internally uses SQLite only.
NoOpCacheEvictor basically defines the eviction mechanism for our cache block we can also use LRU Eviction too .
Now After initializing our download cache we can use that further to generate our Cache Data Source Factory and this can be used while setting up our player instance.
CacheSink: This is a type of Data Sink that has a default fragment & buffer size that is used to write any stream of data to cache.
In case we keep this null our CacheDataSourceFactory would be Read Only
UpstreamFactory: This signifies the data stream from where we will read out data in case there is no cached data i.e directly from the network
DownStreamFactory: This signifies the where or how we are going to read data in terms of local file & cache
Here we are using File Data Source based on our use case but there are many other Data Sources too
FLAG IGNORE CACHE ON ERROR: This is a flag that helps to ignore any error we face in case any cached chunk is not available or gives an error. In such cases, our Up Stream Factory will be used. Similar to this we have 2 other flags too FLAG_BLOCK_ON_CACHE and FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS.
The Cache Data Source Factory now further can be used to build media sources and used along with our Player.
What’s Next?
Some Additional thoughts?
There are many in-depth configurations & experimentation that can be done along with this. This is a demo implementation of how to do basic caching Do let me know in the comments how you have worked on ExoPlayer & its various amazing features.
Thank you for reading, stay tuned for amazing articles!
Catch me up on: https://twitter.com/its_pra_tick
Do not forget claps, happy coding :)