Media3 / ExoPlayer for Kotlin: Complete Guide with Examples
\n\n1. What It Is and Why You Need It
\n\nMedia3 (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.
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.
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\n2. Installation
\n\nMedia3 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:
// 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\nImportant: Versions may change. The latest version can always be found on the official release page.
\n\nAfter adding the dependencies, perform a Gradle sync (Sync Now) in Android Studio.
\n\n3. Quick Start — Minimal Working Example
\n\nLet'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.
Step 1: Layout (activity_main.xml)
\n\n<?xml version="1.0" encoding="utf-8"?>\n<androidx.constrain Also in library
Elasticsearch Client for Java: Complete Guide to Working with Java Client
Kafka Client for Java: Complete Guide with Examples
Geolocation and Maps in PHP: Libraries, API, and Code Examples
Caching in Node.js: Redis, Memcached, and Built-in Memory
Working with Dates in Swift: Foundation, SwiftDate, DateTools (iOS/macOS)