-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Quickstart with Exoplayer
ExoPlayer is Google's open-source, application-level media player for Android. It supports more streaming formats (DASH, SmoothStreaming, HLS) and is far more customizable than the framework MediaPlayer / VideoView.
ExoPlayer now ships as part of AndroidX Media3 under the androidx.media3 artifact group. The legacy com.google.android.exoplayer:exoplayer 2.x artifact (and its com.google.android.exoplayer2.* packages) is no longer maintained — new code should use Media3. See AndroidX Media3 migration guide.
In your project-level settings.gradle (or root build.gradle), make sure Google's Maven repository is available:
repositories {
google()
mavenCentral()
}In your app-level build.gradle, add the Media3 dependencies you need:
dependencies {
def media3Version = "1.10.1"
implementation "androidx.media3:media3-exoplayer:$media3Version"
implementation "androidx.media3:media3-ui:$media3Version"
// Optional: adaptive-streaming format modules
// implementation "androidx.media3:media3-exoplayer-dash:$media3Version"
// implementation "androidx.media3:media3-exoplayer-hls:$media3Version"
}See the Media3 release notes for the current stable version.
The basic steps for playing a remote video with ExoPlayer are:
- Add a
PlayerViewto your layout — this is the view on which the video will be rendered. - Build an
ExoPlayerinstance — this drives streaming, decoding, and rendering. - Wrap the content URL in a
MediaItemand hand it to the player. - Attach the
PlayerViewto the player. - Call
prepare()andplay()on the player.
In your activity or fragment layout XML, add the player view:
<androidx.media3.ui.PlayerView
android:id="@+id/pv_main"
android:layout_width="match_parent"
android:layout_height="300dp" />
<!-- Customization attributes like app:auto_show or app:show_buffering are available
via androidx.media3.ui; see the PlayerView reference. -->In your activity, build and attach the player:
import androidx.media3.common.MediaItem;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.ui.PlayerView;
public class MainActivity extends AppCompatActivity {
private static final String CONTENT_URL =
"https://www.radiantmediaplayer.com/media/bbb-360p.mp4";
private ExoPlayer player;
private PlayerView pvMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pvMain = findViewById(R.id.pv_main);
player = new ExoPlayer.Builder(this).build();
pvMain.setPlayer(player);
MediaItem mediaItem = MediaItem.fromUri(CONTENT_URL);
player.setMediaItem(mediaItem);
player.prepare();
player.play();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (player != null) {
player.release();
player = null;
}
}
}ExoPlayer instances must be accessed from a single application thread (typically the main thread), and releasing the player when the surrounding component is destroyed is required to free decoders. See the Media3 hello world guide.
private void pausePlayer() {
if (player != null) {
player.pause();
}
}
private void playPlayer() {
if (player != null) {
player.play();
}
}
private void seekTo(long positionMs) {
if (player != null) {
player.seekTo(positionMs);
}
}Always release the player when you are done with it, otherwise video decoders remain held and other apps may be unable to play media:
private void releasePlayer() {
if (player != null) {
pvMain.setPlayer(null);
player.release();
player = null;
}
}Extras
- To silence the video, call
player.setVolume(0f). -
PlayerViewhas a built-in controller that you can toggle withpvMain.setUseController(true/false). You can also embed a standaloneandroidx.media3.ui.PlayerControlViewif you need more layout flexibility. - Media3 ships additional modules — DASH, HLS, SmoothStreaming, IMA ads, Cast, exoplayer-rtsp, and more. Browse the full list under
androidx.media3.
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.