What is the MediaPlayer class in Android Studio?

The MediaPlayer class in Android is used to play audio and video files in Android applications. It handles the playback of both local and remote media files and streams. MediaPlayer provides a powerful and easy to use API for controlling media playback in apps.

Some key capabilities offered by MediaPlayer include:

  • Playing local audio files like MP3, WAV, etc.
  • Playing local video files like MP4, MKV, FLV, etc.
  • Streaming remote media over HTTP and RTSP
  • Controlling playback functions like play, pause, stop, seek, etc.
  • Getting duration, current position, buffering status, etc.
  • Handling errors and incomplete downloads gracefully
  • Releasing resources when done playing media

Overall, the MediaPlayer class allows developers to easily integrate audio and video playback into Android apps. It is the primary API used for media playback on Android.

Creating a MediaPlayer Instance

The MediaPlayer class follows the builder pattern, so the first step is to instantiate a new MediaPlayer object using the constructor:


val mediaPlayer = MediaPlayer()


MediaPlayer mediaPlayer = new MediaPlayer();

It’s recommended to provide a context (usually the Activity) and an AudioAttributes object to the constructor so the audio output is configured properly. For example:


val attributes = AudioAttributes.Builder()




val mediaPlayer = MediaPlayer().apply {


setVolume(1.0f, 1.0f)



AudioAttributes attributes = new AudioAttributes.Builder()




MediaPlayer mediaPlayer = new MediaPlayer();


mediaPlayer.setVolume(1.0f, 1.0f);

This configures the mediaPlayer instance with the optimal audio settings for playing music.

MediaPlayer Methods

The MediaPlayer class provides a number of key methods for controlling media playback. Some of the main methods include:

  • setDataSource() – Sets the data source (file path or http/rtsp URL) to use for the media file. This method must be called before prepare() or prepareAsync().

  • prepare() – Initializes the player and starts buffering the media stream. This should be called after setDataSource().

  • start() – Starts or resumes playback of the media.

  • pause() – Pauses playback.

  • reset() – Resets the MediaPlayer to its uninitialized state.

  • release() – Releases resources associated with this MediaPlayer object. This must be called when done with the MediaPlayer.

Other important methods include seekTo() for seeking to a position, getDuration() to retrieve the duration, setVolume() to set volume, setAudioStreamType() to set audio stream type, and stop() to fully stop playback.[1]

These methods allow fine-grained control over media playback in Android.

Setting up Callbacks

The MediaPlayer class provides callback methods that you can implement to receive notifications during the media playback lifecycle. The key callbacks to implement are:

  • OnCompletionListener – Called when playback completes. This allows you to respond accordingly such as restarting playback or moving to the next audio file.
  • OnPreparedListener – Called when the media source is ready for playback. This indicates the MediaPlayer is initialized and you can start playback by calling start().

You implement the listeners by creating a class that extends MediaPlayer.OnCompletionListener and implements MediaPlayer.OnPreparedListener. For example:

class MyMediaPlayerListener implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {

  // Implement callback methods here


Then set the listeners on the MediaPlayer instance:

MediaPlayer mediaPlayer = new MediaPlayer();

When implementing the callbacks, be aware they are invoked on the main UI thread. So avoid long running tasks and use a Handler or AsyncTask if needed. See StackOverflow for more details.

Controlling Playback

The MediaPlayer class provides methods for controlling playback of audio and video files. Some key methods for controlling playback include:

Pausing – To pause playback, call the pause() method on the MediaPlayer instance. This will pause playback at the current position.

Resuming – To resume playback after pausing, call the start() method. This will resume playback from where it was paused.

Stopping – To fully stop playback, call the stop() method. This will stop playback and reset the MediaPlayer to its uninitialized state.

Here is an example of pausing and resuming playback:

mediaPlayer.pause(); // Pause playback

// Do something else...

mediaPlayer.start(); // Resume playback

After calling stop(), the MediaPlayer instance will need to be prepared again before restarting playback.

Seeking to a Position

The MediaPlayer class provides the seekTo() method to allow seeking to a specific position in the media file. This method accepts a single int parameter which is the position to seek to in milliseconds.

For example:

mediaPlayer.seekTo(30000); // Seeks to the 30 second mark

The seekTo() method may not work reliably across all media formats and devices. Some common issues include:

  • Inaccurate seeking – the position seeks to may be slightly off the requested time
  • Seeking not working at all for certain media formats like HLS (.m3u8)
  • Seeking near the end of a media file may not work properly

To help improve seeking accuracy, it is recommended to:

  • Round seek positions to the nearest keyframe for the media codec
  • Handle exceptions and retry seeking if it fails
  • Limit seeking very close to the start or end of media

Additionally, the MediaPlayer.OnSeekCompleteListener can be used to detect when seeking has finished and update the playback position.

Getting Duration and Position

Two key methods for getting media information are getDuration() and getCurrentPosition(). The getDuration() method returns the total duration of the media file in milliseconds. This allows you to determine the full length of the media.

For example:

MediaPlayer mp = MediaPlayer.create(this, Uri.parse(uriOfFile));
int duration = mp.getDuration();

// duration is length in milliseconds

The getCurrentPosition() method returns the current playback position in milliseconds. This allows you to determine how much of the media has been played already. You can use this to update seek bars and show playback progress.

For example:

int currentPosition = mp.getCurrentPosition();
// currentPosition is current playback position in milliseconds

One thing to note is that getDuration() may return 0 if the duration is unknown. So it’s best to call it after playback has started or is prepared. See this StackOverflow thread for more details.

Releasing Resources

It is important to properly release the MediaPlayer resource when you are done using it by calling the release() method. As noted on Stack Overflow, releasing the MediaPlayer releases the audio session ID back to the system. If you do not call release(), the audio session ID will be wasted and can cause issues if you need to play additional audio.

The release() method cleans up the resources and state associated with the MediaPlayer object. According to the Android developer documentation, you must call this method as soon as you’re done with the MediaPlayer to ensure that all associated resources are properly released.

Failing to release the MediaPlayer can lead to wasted system resources and memory leaks. As noted in this Stack Overflow thread, you may encounter crashes or errors if resources are not properly released.

Handling Errors

The MediaPlayer class can throw errors and exceptions during playback that need to be properly handled. Some common errors include:

  • IllegalStateException – Thrown when calling a method at an invalid state.
  • IOException – Usually indicates an invalid path or connectivity issue.
  • Error (1, -1004) – Could not open file.
  • Error (1, -38) – Reached EOS prematurely.

To handle errors gracefully, set an OnErrorListener on the MediaPlayer instance like this:

mediaPlayer.setOnErrorListener(new OnErrorListener() {

  public boolean onError(MediaPlayer mp, int what, int extra) {
    // Handle error here
    return true; 

Checking the what and extra parameters can help identify the cause. For low-level issues, logging exceptions can help troubleshoot problems.

See this StackOverflow thread for more on catching MediaPlayer errors.

Best Practices

When using MediaPlayer, it’s important to follow best practices around performance optimization and proper resource handling. Here are some key tips:

Reduce battery drain by releasing the MediaPlayer when it’s no longer needed, avoiding unnecessary restarts, and minimizing audio and video processing. As noted in the Android developer guide, releasing the MediaPlayer properly ensures resources like audio decoders are freed.

Prevent memory leaks by releasing resources in onStop() or onDestroy(). According to the Deep Dive: MediaPlayer Best Practices article, holding strong references to a MediaPlayer in an activity can lead to leaks.

Avoid unnecessarily high CPU usage. As recommended in the MediaPlayer overview, use setScreenOnWhilePlaying(false) during audio playback to allow the screen to turn off and reduce CPU usage.

Handle errors and edge cases appropriately to avoid crashes. Always check for errors when calling MediaPlayer methods.

Use callbacks like onPrepared() and onCompletion() to control state changes instead of blocking the main thread.

By following these best practices around resource handling, performance, errors, and callbacks, MediaPlayer can be used efficiently in Android apps.

Leave a Reply

Your email address will not be published. Required fields are marked *