Working with MediaPlayer and ExoPlayer in Android Development
In Android development, media playback (audio and video) is a common requirement. Android provides two powerful tools for media playback: the MediaPlayer
and ExoPlayer
. In this article, we'll explore how to use both to handle media in your app with Kotlin.
Using MediaPlayer
The MediaPlayer
class is a built-in Android API that allows you to play audio and video files. It supports local resources and media from the internet.
Step 1: Add Permissions
If you're streaming media over the internet, make sure to add the necessary permissions to your AndroidManifest.xml
file:
<uses-permission android:name="android.permission.INTERNET" />
Step 2: Using MediaPlayer to Play Audio
To use MediaPlayer
, you first need to create an instance, set the data source, and start the playback.
import android.media.MediaPlayer import android.os.Bundle import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private var mediaPlayer: MediaPlayer? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Initialize MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.sample_audio) // Start playing the audio mediaPlayer?.start() } override fun onDestroy() { super.onDestroy() // Release the MediaPlayer when the activity is destroyed mediaPlayer?.release() } }
In this example:
- We initialize a
MediaPlayer
instance with a sample audio file stored in theres/raw
folder. - The audio starts playing automatically when the activity is created.
- We release the
MediaPlayer
when the activity is destroyed to free up resources.
Using ExoPlayer
ExoPlayer
is a more powerful media player that provides support for adaptive streaming (HLS, DASH), better performance, and customizability. It's the recommended player for more complex media tasks.
Step 1: Add Dependencies
To use ExoPlayer, you need to add its dependency to your project. In your build.gradle
(Module: app) file, add the following:
dependencies { implementation 'com.google.android.exoplayer:exoplayer:2.16.1' }
Step 2: Using ExoPlayer to Play Video
ExoPlayer is ideal for handling video playback, especially when streaming media. Here's an example of how to use ExoPlayer to play a video.
import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.MediaItem import com.google.android.exoplayer2.ui.PlayerView class MainActivity : AppCompatActivity() { private lateinit var player: ExoPlayer override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val playerView: PlayerView = findViewById(R.id.playerView) // Initialize ExoPlayer player = ExoPlayer.Builder(this).build() // Set the media item (video URL or local file) val mediaItem = MediaItem.fromUri("https://www.example.com/sample_video.mp4") player.setMediaItem(mediaItem) // Prepare and start the player player.prepare() player.play() // Bind the player to the PlayerView playerView.player = player } override fun onPause() { super.onPause() // Release the player when the activity is paused player.pause() } override fun onDestroy() { super.onDestroy() // Release the ExoPlayer instance player.release() } }
Layout XML for PlayerView
<com.google.android.exoplayer2.ui.PlayerView android:id="@+id/playerView" android:layout_width="match_parent" android:layout_height="wrap_content" />
In this example:
- We use the
ExoPlayer
to create a player instance and configure it with a video source. - We initialize the
PlayerView
in the layout to display the video. - The video starts playing as soon as the player is prepared.
- We release the player resources when the activity is paused or destroyed to avoid memory leaks.
Step 3: Handling Playback Events
ExoPlayer provides several playback events like buffering, state changes, etc. You can listen to these events by using a Player.Listener
.
player.addListener(object : Player.Listener { override fun onPlaybackStateChanged(state: Int) { when (state) { Player.STATE_READY -> { // Player is ready to play } Player.STATE_BUFFERING -> { // Player is buffering } Player.STATE_ENDED -> { // Playback ended } } } })
Comparison of MediaPlayer and ExoPlayer
While both MediaPlayer
and ExoPlayer
allow you to play audio and video, ExoPlayer
offers more advanced features such as:
- Support for adaptive streaming protocols like HLS, DASH, and Smooth Streaming.
- Better performance for large files or high-definition content.
- More flexibility for adding custom behaviors like skipping, fast-forward, or playback speed adjustments.
For simple use cases such as playing local audio or video files, MediaPlayer
might be sufficient. However, if you're dealing with streaming or need more advanced features, ExoPlayer
is the better choice.
Conclusion
Both MediaPlayer
and ExoPlayer
are excellent choices for playing media in your Android app. MediaPlayer
is simpler and suitable for most cases, while ExoPlayer
offers more control, flexibility, and performance for complex media playback scenarios, especially when dealing with streaming content.