ExoPlayer is an app-level media player which is very similar to the MediaPlayer. Unlike MediaPlayer, ExoPlayer is built on top of low-level media APIs in Android which has many advantages like it supports DASH and SmoothStreaming adaptive playbacks. ExoPlayer is highly customizable and extensible, making it capable of many advanced use cases. It is an open-source project used by Google apps, including YouTube and Google Play Movies and TV. In this article, we will show you how you could Implement ExoPlayer to Play a Video in Android using Jetpack Compose. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Add INTERNET permission in the AndroidManifest.xml file
Go to the AndroidManifest.xml file and add INTERNET permission as shown below.
<? xml version = "1.0" encoding = "utf-8" ?>
package = "com.geeksforgeeks.jcvideo" >
<!-- Add this permission -->
< uses-permission android:name = "android.permission.INTERNET" />
< application
android:allowBackup = "true"
android:icon = "@mipmap/ic_launcher"
android:label = "@string/app_name"
android:roundIcon = "@mipmap/ic_launcher_round"
android:supportsRtl = "true"
android:theme = "@style/Theme.JCVideo" >
< activity
android:name = ".MainActivity"
android:exported = "true"
android:label = "@string/app_name"
android:theme = "@style/Theme.JCVideo" >
< intent-filter >
< action android:name = "android.intent.action.MAIN" />
< category android:name = "android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
</ application >
</ manifest >
|
Step 3: Add ExoPlayer Dependency in build.gradle (app) file
Go to app build.gradle file and add this dependency in the dependency section. Always add the latest version of the dependency for the proper functioning of the application. ExoPlayer’s latest dependency can be found here: RELEASE NOTES
// Exo Player Library implementation "com.google.android.exoplayer:exoplayer:2.16.1"
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
package com.geeksforgeeks.jcvideo
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.ui.PlayerView
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.util.Util
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContent {
// Calling the composable function
// to display element and its contents
MainContent()
}
}
} // Creating a composable // function to display Top Bar @Composable fun MainContent() { Scaffold(
topBar = { TopAppBar(title = { Text( "GFG | ExoPlayer Video" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) },
content = { MyContent() }
)
} // Creating a composable function to create // two Images and a spacer between them // Calling this function as content in the above function @Composable fun MyContent(){ Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
// Fetching the Local Context
val mContext = LocalContext.current
// Declaring a string value
// that stores raw video url
val mVideoUrl = "https://cdn.videvo.net/videvo_files/video/free/2020-05/large_watermarked/3d_ocean_1590675653_preview.mp4"
// Declaring ExoPlayer
val mExoPlayer = remember(mContext) {
ExoPlayer.Builder(mContext).build().apply {
val dataSourceFactory = DefaultDataSourceFactory(mContext, Util.getUserAgent(mContext, mContext.packageName))
val source = ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(mVideoUrl))
prepare(source)
}
}
// Implementing ExoPlayer
AndroidView(factory = { context ->
PlayerView(context).apply {
player = mExoPlayer
}
})
}
} // For displaying preview in // the Android Studio IDE emulator @Preview (showBackground = true )
@Composable fun DefaultPreview() { MainContent()
} |
Output:
In the below video, you can see that the ExoPlayer is successfully implemented.