Skip to content

Repository files navigation

AndroidPlay

A exoplayer based Media player for Android with Interactive Media Ads

Dependencies [build.gradle (Module: app)]

dependencies {
//Other dependenices//Exoplayer
implementation "com.google.android.exoplayer:exoplayer:$exoPlayerVersion"//Exoplayer IMA(Interactive Media Ads)
implementation ("com.google.android.exoplayer:extension-ima:$exoPlayerVersion"){
exclude module:'support-v4'
}
}

Setting up Player

classAndroidPlayer(privatevarcontext:Context, privatevalplayerView:PlayerView){
privatevar player :ExoPlayer?=nullprivatevar playbackPosition :Long=0privatevar currentWindowIndex:Int=0privatevar playWhenReady:Boolean=trueprivatevar currUri:Uri?=nullprivatevar currUriList:Array<Uri>?=nullprivateval lifecycleObserver =PlayerLifecycleObserver(this)
init {
while (context !isLifecycleOwner) {
context = (context asContextWrapper).baseContext
}
lifecycleObserver.registerLifecycle((context asLifecycleOwner).lifecycle)
adView = (playerView).overlayFrameLayout
}
//Play single videofunplay(uri:Uri?){
if(uri ==null) return
currUri = uri
initPlayer()
preparePlayer(currUri!!)
hideSystemUi()
}
//Overloaded function to play the whole playlistfunplay(uriList:Array<Uri>?){
if(uriList ==null) return
currUriList = uriList
initPlayer()
preparePlayer(currUriList!!)
hideSystemUi()
}
privatefuninitPlayer(){
if(player ==null){
player =ExoPlayerFactory.newSimpleInstance(
DefaultRenderersFactory(context), DefaultTrackSelector(), DefaultLoadControl()
)
loadState()
playerView.player = player
}else{
loadState()
}
}
//Build MediaSource for one video and prepare playerprivatefunpreparePlayer(uri:Uri){
val mediaSource =MediaSourceBuilder().build(uri)
player?.prepare(mediaSource, true, false)
}
//Overloaded function to build MediaSource for whole playlist and prepare playerprivatefunpreparePlayer(uriList:Array<Uri>){
val mediaSource =MediaSourceBuilder().build(uriList)
player?.prepare(mediaSource, true, false)
}
privatefunsaveState(){
if (player !=null) {
playbackPosition = player?.currentPosition ?:0L
currentWindowIndex = player?.currentWindowIndex ?:0
playWhenReady = player?.playWhenReady ?:true
}
}
privatefunloadState(){
player?.apply {
playWhenReady = playWhenReady
seekTo(currentWindowIndex, playbackPosition)
}
}
privatefunreleasePlayer() {
if (player !=null) {
saveState()
player?.release()
player =null
}
}
//region Handle Lifecyclefunstart() {
player?.playWhenReady =true
player?.playbackState
currUri?: play(currUri)
currUriList?: play(currUriList)
}
funresume(){
player?.playWhenReady =true
currUri?: play(currUri)
currUriList?: play(currUriList)
loadState()
}
funpause() {
player?.playWhenReady =false
player?.playbackState
saveState()
}
funstop(){
releasePlayer()
imaAdsLoader?.release()
player?.stop(true)
}
//endregion
@SuppressLint("InlinedApi")
privatefunhideSystemUi() {
playerView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILEorView.SYSTEM_UI_FLAG_FULLSCREENorView.SYSTEM_UI_FLAG_LAYOUT_STABLEorView.SYSTEM_UI_FLAG_IMMERSIVE_STICKYorView.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATIONorView.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
}
}

Building MediaSourceBuilder

classMediaSourceBuilder {
//Build various MediaSource depending upon the type of Media for a given video urifunbuild(uri:Uri): MediaSource {
val userAgent =PlayerConstants.USER_AGENTval lastPath = uri.lastPathSegment?:""val defaultHttpDataSourceFactory =DefaultHttpDataSourceFactory(userAgent)
if(lastPath.contains(PlayerConstants.FORMAT_MP3) || lastPath.contains(PlayerConstants.FORMAT_MP4)){
returnExtractorMediaSource.Factory(defaultHttpDataSourceFactory)
.createMediaSource(uri)
}elseif(lastPath.contains(PlayerConstants.FORMAT_M3U8)){
returnHlsMediaSource.Factory(defaultHttpDataSourceFactory)
.createMediaSource(uri)
}else{
val dashChunkSourceFactory =DefaultDashChunkSource.Factory(
defaultHttpDataSourceFactory
)
returnDashMediaSource.Factory(dashChunkSourceFactory, defaultHttpDataSourceFactory
)
.createMediaSource(uri)
}
}
//Overloaded function to Build various MediaSource for whole playlist of video urifunbuild(uriList:Array<Uri>) : MediaSource{
val playlistMediaSource =DynamicConcatenatingMediaSource()
uriList.forEach { playlistMediaSource.addMediaSource(build(it)) }
return playlistMediaSource
}
}

Handling Player Lifecycle

classPlayerLifecycleObserver(privatevalplayer:AndroidPlayer) : LifecycleObserver{
funregisterLifecycle(lifecycle:Lifecycle){
lifecycle.addObserver(this)
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
funonCreate(){
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
funonStart(){
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
funonPause(){
player.pause()
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
funonResume(){
player.resume()
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
funonStop(){
player.stop()
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
funonDestroy(){
}
}

Using AndroidPlayer

Setting up Layout for the video player

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity">
<com.google.android.exoplayer2.ui.PlayerView android:id="@+id/videoView"android:layout_height="wrap_content"android:layout_width="match_parent" />
</android.support.constraint.ConstraintLayout>

Calling Player from Activity

classMainActivity : AppCompatActivity() {
//Other codeprivateenumclassPlayMode { SINGLE_VIDEO, PLAY_LIST }
privatevar playMode =PlayMode.SINGLE_VIDEOprivatefunstartDemoOfAndroidPlayer(){
val videoUrl ="https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"val uri =Uri.parse(videoUrl)
val uriList = arrayOf<Uri>(uri,uri,uri,uri)
when(playMode){
PlayMode.SINGLE_VIDEO-> playVideo(uri)
PlayMode.PLAY_LIST-> playVideoList(uriList)
}
}
privatefunplayVideo(uri:Uri){
//Here the videoView is AndroidPlayer(this, videoView).play(uri)
}
privatefunplayVideoList(uriList:Array<Uri>){
AndroidPlayer(this, videoView).play(uriList)
}
}

Additional Resources

Happy Coding!

About

A exoplayer based Media player for Android

Topics

Resources

Stars

16 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages