Media3 / ExoPlayer for Kotlin: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Media3 / ExoPlayer for Kotlin: Complete Guide with Examples

\n\n

1. What It Is and Why You Need It

\n\n

Media3 (formerly known as ExoPlayer) is a modern, powerful, and flexible library for audio and video playback on the Android platform, developed by Google. It is the successor to the outdated MediaPlayer from the Android SDK and offers significantly broader functionality, better performance, and control over the playback process.

\n\n

Why do you need Media3 / ExoPlayer? Unlike the standard MediaPlayer, which is only suitable for simple tasks, ExoPlayer gives the developer full control over the media pipeline. It supports many formats (DASH, HLS, SmoothStreaming, MP4, MP3, WebM, etc.), adaptive bitrate switching (ABR), customizable rendering, subtitle support (including WebVTT, TTML), DRM protection (Widevine, PlayReady), and much more. The library is actively developed, optimized, and used in applications such as YouTube, Google Photos, and many other popular services.

\n\n

For Kotlin developers, Media3 is the de facto standard for creating any media applications: from a simple music player to a complex video player with adaptive streaming and DRM support. It allows you to write clean, modular, and testable code, fully controlling the playback lifecycle.

\n\n

2. Installation

\n\n

Media3 is an Android library, so it is installed via the Gradle build system. Add the following dependencies to your module's build.gradle.kts (or build.gradle) file for app:

\n\n
// build.gradle.kts (Module :app)\ndependencies {\n    // Base Media3 library (required)\n    implementation("androidx.media3:media3-exoplayer:1.4.1")\n    // UI components (PlayerView, PlayerControlView, etc.)\n    implementation("androidx.media3:media3-ui:1.4.1")\n    // For HLS (HTTP Live Streaming)\n    implementation("androidx.media3:media3-exoplayer-hls:1.4.1")\n    // For DASH (Dynamic Adaptive Streaming over HTTP)\n    implementation("androidx.media3:media3-exoplayer-dash:1.4.1")\n    // For SmoothStreaming\n    implementation("androidx.media3:media3-exoplayer-rtsp:1.4.1")\n    // For subtitles\n    implementation("androidx.media3:media3-exoplayer-ima:1.4.1")\n    // For DRM (Widevine)\n    implementation("androidx.media3:media3-exoplayer-drm:1.4.1")\n    // For sessions (MediaSession) — required for integration with system controls\n    implementation("androidx.media3:media3-session:1.4.1")\n    // For custom data sources (e.g., caching)\n    implementation("androidx.media3:media3-datasource:1.4.1")\n    // For custom codecs (e.g., FFmpeg)\n    implementation("androidx.media3:media3-decoder:1.4.1")\n    // For custom effects (e.g., speed change)\n    implementation("androidx.media3:media3-effect:1.4.1")\n    // For custom transformations (e.g., video cropping)\n    implementation("androidx.media3:media3-transformer:1.4.1")\n}\n
\n\n

Important: Versions may change. The latest version can always be found on the official release page.

\n\n

After adding the dependencies, perform a Gradle sync (Sync Now) in Android Studio.

\n\n

3. Quick Start — Minimal Working Example

\n\n

Let's create a simple video player that plays a video from the internet. For this, we need a PlayerView in the XML layout and code in an Activity/Fragment.

\n\n

Step 1: Layout (activity_main.xml)

\n\n
<?xml version="1.0" encoding="utf-8"?>\n<androidx.constrain                    

Recommendations