Android Exoplayer using Kotlin
Last Updated :
18 Apr, 2025
ExoPlayer View is one of the most used UI components in media streaming applications for displaying video files within android applications. It is similar to that of Video View, but the quality of the video player in Exoplayer compared to video view is better. In this article, we will look at How to use Exoplayer View in android using Kotlin.
Note: If you are looking to implement ExoPlayer View in Android using Java. Check out the following article: ExoPlayer View in Android using Java
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.
Note that select Kotlin as the programming language.
Step 2: Add dependency to the build.gradle.kts (Module:app)
Navigate to the Gradle Scripts > build.gradle.kts (Module:app) and add the below dependency in the dependencies section.
dependencies {
...
val exoplayerVersion = "1.4.1"
// Core ExoPlayer library
implementation("androidx.media3:media3-exoplayer:$exoplayerVersion")
// Common utilities (recommended)
implementation("androidx.media3:media3-common:$exoplayerVersion")
// UI components
implementation("androidx.media3:media3-ui:$exoplayerVersion")
// DASH support
implementation("androidx.media3:media3-exoplayer-dash:$exoplayerVersion")
// HLS support
implementation("androidx.media3:media3-exoplayer-hls:$exoplayerVersion")
// SmoothStreaming support
implementation("androidx.media3:media3-exoplayer-smoothstreaming:$exoplayerVersion")
// Optional: Extractor (for progressive formats like MP4, MP3)
implementation("androidx.media3:media3-exoplayer-rtsp:$exoplayerVersion")
}
After adding this dependency simply sync your project to install them.
Step 3: Add internet permission to your Manifest file
Navigate to the app > manifest folder and write down the following permissions to it.
<uses-permission android:name=”android.permission.INTERNET”/>
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
activity_main.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--on below line we are creating
a text for heading of our app-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp"
android:text="Exo Player View"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
<!--Widget for exoplayer view-->
<androidx.media3.ui.PlayerView
android:id="@+id/idExoPlayerVIew"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Step 5: 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.
MainActivity.kt:
Kotlin
package org.geeksforgeeks.demo
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
class MainActivity : AppCompatActivity() {
// ExoPlayer instance
private lateinit var exoPlayer: ExoPlayer
// PlayerView from the layout
private lateinit var playerView: PlayerView
// Video URL to be played
private val videoURL = "https://media.geeksforgeeks.org/wp-content/uploads/20201217163353/Screenrecorder-2020-12-17-16-32-03-350.mp4"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Find the PlayerView from layout
playerView = findViewById(R.id.idExoPlayerVIew)
// Initialize the ExoPlayer
exoPlayer = ExoPlayer.Builder(this).build()
// Attach the player to the PlayerView
playerView.player = exoPlayer
// Create a MediaItem from the video URL
val mediaItem = MediaItem.fromUri(Uri.parse(videoURL))
// Set the media item to the player
exoPlayer.setMediaItem(mediaItem)
// Set repeat mode
exoPlayer.repeatMode = ExoPlayer.REPEAT_MODE_ALL
// Prepare the player (loads media)
exoPlayer.prepare()
// Automatically start playback when ready
exoPlayer.playWhenReady = true
}
override fun onStop() {
super.onStop()
// Release player when the activity is no longer visible
exoPlayer.release()
}
}
Output:
Similar Reads
Kotlin Flow in Android with Example Kotlin Flow is a tool that helps developers work with data that changes over time like search results, live updates, or user input. Itâs part of Kotlinâs coroutines, which are used for writing code that doesnât block the app while waiting for something to finish, like a network call or a file to loa
8 min read
Kotlin Android Extensions If youâve been developing Android Apps for some time, youâre probably already tired of working with findViewById in your day-to-day life to recover views. Or maybe you gave up and started using the famous Butterknife library. If thatâs your case, then youâll love Kotlin Android Extensions. Kotlin ha
2 min read
Flutter vs Kotlin - For Android Development Believe it or not but the future is going to be in the hands of compact devices such as mobile phones, tablets, etc. In today's tech-driven era, every single requirement is being fulfilled with the help of smartphones, and the medium used is Mobile applications. Android development is like being a d
7 min read
Kotlin Android Tutorial Kotlin is a cross-platform programming language that may be used as an alternative to Java for Android App Development. Kotlin is an easy language so that you can create powerful applications immediately. Kotlin is much simpler for beginners to try as compared to Java, and this Kotlin Android Tutori
6 min read
Android ListView in Kotlin ListView in Android is a ViewGroup which is used to display a scrollable list of items arranged in multiple rows. It is attached to an adapter which dynamically inserts the items into the list. The main purpose of the adapter is to retrieve data from an array or a database and efficiently push every
3 min read
How to Send SMS in Android using Kotlin? SMS Manager is a class in Android which is used to send the SMS to a specific contact from the android application. We can send text messages, data messages, and multimedia messages using this class. There are different methods that are provided to send different types of messages. In this article,
4 min read