This repository demonstrates how to use ExoPlayer in a Jetpack Compose application. It includes three different player implementations:
- Online Player: Streams video from an online URL.
- Raw Player: Plays a video stored in the raw resources of the app.
- YouTube Player: Embeds a YouTube video using a
WebView
.
- Android Studio with Jetpack Compose setup
- ExoPlayer dependency in your
build.gradle
file - Internet permissions if using the Online Player
-
Clone the repository:
git clone https://github.com/Bhavyansh03-tech/Exo_Player.git
-
Open the project in Android Studio.
-
Build the project and run it on an emulator or a physical device.
-
Add Dependencies
Make sure you have the following dependencies in your
build.gradle
file:[libraries] # YOUTUBE PLAYER :-> androidx-webkit = { module = "androidx.webkit:webkit", version.ref = "webkit" } # MEDIA EXO PLAYER :-> androidx-media3-exoplayer = { module = "androidx.media3:media3-exoplayer", version.ref = "media3Exoplayer" } androidx-media3-exoplayer-hls = { module = "androidx.media3:media3-exoplayer-hls", version.ref = "media3Exoplayer" } androidx-media3-ui = { module = "androidx.media3:media3-ui", version.ref = "media3Exoplayer" }
-
Add Internet Permission (for Online Player)
In your
AndroidManifest.xml
, add the following permission:<uses-permission android:name="android.permission.INTERNET"/>
Streams video from an online URL.
// Media Item :->
val mediaItem = MediaItem.fromUri("https://cdn.pixabay.com/video/2024/04/18/208442_large.mp4") // Apply Changes.
// Media source :->
val mediaSource: MediaSource = ProgressiveMediaSource.Factory(DefaultHttpDataSource.Factory()) // Add this.
.createMediaSource(mediaItem)
val exoPlayer = remember {
ExoPlayer.Builder(context).build().apply {
setMediaSource(mediaSource) // Apply Changes.
prepare()
playWhenReady = true
}
}
Plays a video stored in the raw resources of the app.
@Composable
fun RawPlayer() {
// Lifecycle :->
var lifecycle by remember {
mutableStateOf(Lifecycle.Event.ON_CREATE)
}
// Get context :->
val context = LocalContext.current
// Media Item :->
val mediaItem = MediaItem.fromUri("android.resource://${context.packageName}/${R.raw.sample}")
// Media source :->
val exoPlayer = remember {
ExoPlayer.Builder(context).build().apply {
setMediaItem(mediaItem)
prepare()
playWhenReady = true
}
}
// Lifecycle for composable :->
val lifecycleOwner = LocalLifecycleOwner.current
DisposableEffect(key1 = lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
lifecycle = event
}
lifecycleOwner.lifecycle.addObserver(observer)
onDispose {
exoPlayer.release()
lifecycleOwner.lifecycle.removeObserver(observer)
}
}
// Android View :->
AndroidView(
modifier = Modifier
.fillMaxWidth()
.aspectRatio(16f / 9f),
factory = {
PlayerView(context).also { playerView ->
playerView.player = exoPlayer
}
},
update = {
when (lifecycle){
Lifecycle.Event.ON_RESUME -> {
it.onPause()
it.player?.pause()
}
Lifecycle.Event.ON_PAUSE -> {
it.onResume()
}
else -> Unit
}
}
)
}
Embeds a YouTube video using a WebView
.
@Composable
fun YoutubePlayer(videoId: String) {
AndroidView(
modifier = Modifier.fillMaxSize().padding(top = 25.dp),
factory = { context ->
WebView(context).apply {
webViewClient = WebViewClient()
settings.javaScriptEnabled = true
loadUrl("https://youtu.be/$videoId")
}
}
)
}
Contributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/your-feature
). - Commit your changes (
git commit -am 'Add some feature'
). - Push to the branch (
git push origin feature/your-feature
). - Create a new Pull Request.
For questions or feedback, please contact @Bhavyansh03-tech on GitHub or connect with me on LinkedIn.