How to migrate to ExoPlayer from an older API?

Migrating from Older API to ExoPlayer

ExoPlayer is a powerful and flexible media player for Android that offers more features and better performance than the legacy media APIs. Migrating to ExoPlayer from the older media framework can be a straightforward process if you follow a few simple steps.

Step 1: Add ExoPlayer Dependency

First, you need to add ExoPlayer to your project. Open your app's build.gradle file and add the following dependency:

implementation 'com.google.android.exoplayer:exoplayer:2.x.x'

Step 2: Create ExoPlayer Instance

Next, create an instance of ExoPlayer. You can initialize it in your activity or fragment:

SimpleExoPlayer player = new SimpleExoPlayer.Builder(context).build();

Step 3: Prepare Media Source

You need to create a MediaSource for the media you want to play. Here’s a basic setup:

Uri uri = Uri.parse("http://your_media_url.mp4"); MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory) .createMediaSource(MediaItem.fromUri(uri));

Step 4: Prepare and Start Playback

Once your MediaSource is ready, prepare and start the player:

player.setMediaSource(mediaSource); player.prepare(); player.play();

Step 5: Release Resources

Finally, don't forget to release the player when it's no longer needed to free up resources:

player.release();

ExoPlayer Android media player migrate to ExoPlayer Android audio player ExoPlayer tutorial