Media Player Android

The MediaPlayer class in Android is used to play and control audio files.

MediaPlayer class:

For controlling the audio or video files, the android.media.MediaPlayer class is used.

Methods of MediaPlayer class:

The MediaPlayer class contains many methods. Some of the important methods of the MediaPlayer class are:

Method Uses
public void setDataSource(String path) It is used to set the data source, i.e., file path or http url, to use.
public void prepare() It is used to prepare the player for playback synchronously.
public void start() It is used to start or resume the playback.
public void stop() It is used to stop the playback.
public void pause() It is used to pause the playback.
public boolean isPlaying() It is used to check if the media player is playing.
public void seekTo(int millis) It is used to seek a specific time in milliseconds.
public void setLooping(boolean looping) It is used to set the player for looping or non-looping.
public boolean isLooping() It is used to check if the player is looping or non-looping.
public void selectTrack(int index) It is used to select a track for the specified index.
public int getCurrentPosition() It is used to return the current playback position.
public int getDuration() It is used to return the duration of the file.
public void setVolume(float leftVolume,float rightVolume) It is used to set the volume on this player.

Example: To control the audio:

In the below example, we will demonstrate how to start, stop, and pause the audio play.

activity_main.xml:

In the activity_main.xml file, we will drag three buttons from the palette to start, stop, and to pause the audio play.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="30dp"
        android:text="Play your Favorite Music......" />
 
    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="48dp"
        android:text="play" />
 
    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button1"
        android:layout_toRightOf="@+id/button1"
        android:text="pause" />
 
    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button2"
        android:layout_toRightOf="@+id/button2"
        android:text="stop" />
 
</RelativeLayout>

Activity class:(File: MainActivity.java)

In the MainActivity.java file, we will write the code to start, pause, and to stop the audio player.

package com.example.radioapp;
 
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity {
    Button start,pause,stop;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        start=(Button)findViewById(R.id.button1);
        pause=(Button)findViewById(R.id.button2);
        stop=(Button)findViewById(R.id.button3);
        //creating media player
        final MediaPlayer mp=new MediaPlayer();
        try{
            //you can change the path, here path is external directory(e.g. sdcard) /Music/maine.mp3
            mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Music/musicplay.mp3");
 
            mp.prepare();
        }catch(Exception e){e.printStackTrace();}
 
        start.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.start();
            }
        });
        pause.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.pause();
            }
        });
        stop.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.stop();
            }
        });
    }
}

Output: