Open In App

Android Exoplayer using Kotlin

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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: 



Next Article
Article Tags :

Similar Reads