Skip to content

Instantly share code, notes, and snippets.

@rohanprasadofficial
Created June 14, 2020 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohanprasadofficial/7851f07444e8efc8e9dba3ab6bba0131 to your computer and use it in GitHub Desktop.
Save rohanprasadofficial/7851f07444e8efc8e9dba3ab6bba0131 to your computer and use it in GitHub Desktop.
Summary of the implementation of the Exoplayer (By Google ) and VideoCache (https://github.com/danikula/AndroidVideoCache)
//ADD the dependencies to the project
//Billing Library
implementation 'com.android.billingclient:billing:2.2.1'
implementation 'com.android.billingclient:billing-ktx:2.2.0'
implementation 'com.danikula:videocache:2.7.1'
//Proxy function to create video caching in the app
private fun getProxy(): HttpProxyCacheServer? {
proxy= HttpProxyCacheServer(applicationContext)
if(proxy==null)
{
proxy = HttpProxyCacheServer.Builder(applicationContext)
.headerInjector(UserAgentHeadersInjector())
.build()
}
return proxy
}
//Main Logic
try {
val url="https//exmapleapi.com/mp4file.mp4;
val proxy :HttpProxyCacheServer? = getProxy()
val proxyUrl = proxy?.getProxyUrl(url)
val bandwidthMeter = DefaultBandwidthMeter.Builder(this).build()
val loadControl = DefaultLoadControl
.Builder()
.setBufferDurationsMs(10000,10000,10000,10000)
.createDefaultLoadControl()
val player = SimpleExoPlayer.Builder(applicationContext).setLoadControl(loadControl).setBandwidthMeter(bandwidthMeter).build()
binding.videoView.player = player
player.audioComponent?.volume = 0f
val mediaSource:MediaSource = buildMediaSource(Uri.parse(proxyUrl));
player.repeatMode= Player.REPEAT_MODE_ONE
player.prepare(mediaSource)
player.playWhenReady = true
}catch (e:Exception)
{e.printStackTrace()
}
//Building media source
private fun buildMediaSource(uri: Uri): MediaSource {
fun createSource1() : DefaultDataSourceFactory {
val dsf = DefaultDataSourceFactory(this@ExoPlayer,Util.getUserAgent(applicationContext,application.getString(R.string.app_name)))
return dsf
}
val myDSFactory=createSource1()
val emf = ProgressiveMediaSource.Factory(myDSFactory)
return emf.createMediaSource(uri)
}
//Inner class of Exoplayer class
inner class UserAgentHeadersInjector : HeaderInjector {
override fun addHeaders(url: String?): MutableMap<String, String> {
return mutableMapOf("User-Agent" to "App-Name","Authorization" to "Bearer "+ "ACCESS_TOKEN")
}
}
@rohanprasadofficial
Copy link
Author

I found a lot of issues working with the ExoPlayer as having deprecated documentation as of now and integrating the video caching in the app to improve performance and reduce bandwidth usage.
So I created this gist to help people with basic snippets.
Special thanks to (https://github.com/danikula/AndroidVideoCache) for this library.
You can use this as a reference for the caching in Exoplayer but if you are implementation DASH then you won't need caching as its defaults in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment