Skip to main content

MediaPlayer Android

MediaPlayer Examples.


MediaPlayer is a class in Android that provides playback of audio and video files. It can be used to play audio or video from resources, files, streams, or remote URLs.

In Kotlin, MediaPlayer can be initialized using the MediaPlayer() constructor. Once initialized, you can set the data source using setDataSource() method, which can accept various types of inputs like file path, resource ID, or URL. After setting the data source, you need to call prepare() or prepareAsync() method to prepare the MediaPlayer for playback.

MediaPlayer provides various methods for controlling playback such as start(), pause(), stop(), seekTo(), and release(). You can also register listeners to handle events like completion, buffering, or errors.

To play audio or video using MediaPlayer, you need to acquire the appropriate permissions in the AndroidManifest.xml file. For example, if you want to play audio from the internet, you need to include the "INTERNET" permission.

How to play Audio Files using MediaPlayer

Here is a step-by-step tutorial on how to play audio files using MediaPlayer in Kotlin Android:

Step 1: Add the necessary permissions and dependencies

Before we start, make sure you have the following permissions added in your AndroidManifest.xml file:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Next, add the following dependency to your app-level build.gradle file:

implementation 'androidx.appcompat:appcompat:1.3.0'

Step 2: Create a layout file

Create a layout file (e.g., activity_main.xml) that contains a Button to start playing the audio file:

android:text="Play Audio" />

Step 3: Initialize the MediaPlayer

In your activity or fragment, initialize a MediaPlayer object:

val mediaPlayer = MediaPlayer()

Step 4: Set the data source

Set the data source of the MediaPlayer to the audio file you want to play. In this example, we will play an audio file from the device's external storage:

val audioFile = File(Environment.getExternalStorageDirectory(), "audio.mp3")

Step 5: Prepare the MediaPlayer

Before playing the audio, you need to prepare the MediaPlayer by calling the prepare() or prepareAsync() method. If you use prepare(), it will block the UI until the audio is ready to play. If you use prepareAsync(), it will play the audio asynchronously without blocking the UI:

mediaPlayer.prepare()  // or mediaPlayer.prepareAsync()

Step 6: Implement the play button click event

In your activity or fragment, set an OnClickListener to the play button and start playing the audio when the button is clicked:

val btnPlay = findViewById<Button>(
btnPlay.setOnClickListener {

Step 7: Handle the MediaPlayer lifecycle

To ensure proper resource release and avoid memory leaks, you should handle the lifecycle of the MediaPlayer. For example, you can release the MediaPlayer when the activity or fragment is destroyed:

override fun onDestroy() {

That's it! You have successfully implemented audio playback using MediaPlayer in Kotlin Android. You can now run your app and click the play button to start playing the audio file.

Additional Tips:

  • You can add additional features such as pause, stop, or seek functionality by using the corresponding methods provided by the MediaPlayer class.
  • Remember to handle exceptions when using MediaPlayer methods, as they may throw IOExceptions or IllegalStateExceptions.
  • If you want to play audio from a different source (e.g., from a raw resource or a network URL), you can use different methods to set the data source accordingly.

I hope this tutorial helps you in implementing audio playback in your Kotlin Android app using MediaPlayer.