Android ExoPlayer Example in Kotlin

This post is based on implementing Exoplayer Library to play media files on Android.

ExoPlayer 

  • ExoPlayer is an open source library maintained by Google.
  • It  supports a wide range of media files including DASH and HLS streaming which is not supported by the MediaPlayer.
  • It provides you the ability to customize your media player according to your requirements.
  • ExoPlayer provides the support for the playlist and also provides smooth encryption and streaming of video and audio files.

Let’s create a simple video player app in which we will be fetching a video from a URL and play that video inside our ExoPlayer. 

Creating new Project

1 . Create a new project by going to File  New Android Project, select Empty Activity, provide app name, select language to kotlin and then finally click on finish.

2 . Open app-level build.gradle file, add the dependency of ExoPlayer in the dependencies section, and sync the project.

build.gradle

dependencies {
//ExoPlayer dependency
implementation 'com.google.android.exoplayer:exoplayer:2.15.0'
}

3. Go to AndroidManifest.xml and add the internet permission above <application> tag as shown below:

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>

4. Open the layout file activity_main.xml and add the PlayerView as shown below:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/playerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

5. Open MainActivity.kt and add the below code:

MainActivity.kt

package com.c1ctech.exoplayerdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.android.exoplayer2.*
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.ui.PlayerView
import com.google.android.exoplayer2.util.Util
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.upstream.*
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource

class MainActivity : AppCompatActivity(), Player.Listener {

private var mPlayer: SimpleExoPlayer? = null
private lateinit var playerView: PlayerView

private val videoURL =
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

//get PlayerView by its id
playerView = findViewById(R.id.playerView)

}

private fun initPlayer() {

// Create a player instance.
mPlayer = SimpleExoPlayer.Builder(this).build()

// Bind the player to the view.
playerView.player = mPlayer

//setting exoplayer when it is ready.
mPlayer!!.playWhenReady = true

// Set the media source to be played.
mPlayer!!.setMediaSource(buildMediaSource())

// Prepare the player.
mPlayer!!.prepare()

}

override fun onStart() {
super.onStart()
if (Util.SDK_INT >= 24) {
initPlayer()
}
}

override fun onResume() {
super.onResume()
if (Util.SDK_INT < 24 || mPlayer == null) {
initPlayer()
}
}

override fun onPause() {
super.onPause()
if (Util.SDK_INT < 24) {
releasePlayer()
}
}

override fun onStop() {
super.onStop()
if (Util.SDK_INT >= 24) {
releasePlayer()
}
}


private fun releasePlayer() {
if (mPlayer == null) {
return
}
//release player when done
mPlayer!!.release()
mPlayer = null
}

//creating mediaSource
private fun buildMediaSource(): MediaSource {
// Create a data source factory.
val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()

// Create a progressive media source pointing to a stream uri.
val mediaSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(MediaItem.fromUri(videoURL))

return mediaSource
}
}

In the above code, getting started with ExoPlayer consists of implementing the following steps:

  1. Create a SimpleExoPlayer instance.
  2. Attach the player to a PlayerView.
  3. Set player with MediaSource and then prepare the player.
  4. Release the player when done.

6. When you run the app it will look like this as shown below:

Leave a Reply