Android MediaPlayer Tutorial

View more Tutorials:

1- MediaPlayer

Android provides a component  to play music, it's MediaPlayer. MediaPlayer can run audio and video files, the source file is located on your device or from a URL.

Just like with other music player that you know, MediaPlayer provides methods to control playback of audio/video, including start, stop, rewind .....
You can also call MediaPlayer  from a service.

MediaPlayer is a component that don't have interface, it help you play a music file easily, but to play a video file you need to combine it with SuffaceView to display imagine. 

2- MediaPlayer example

The following simple example, use Media Player to play a music file and some controls of the play such as play, pause, rewind.
Create a project named MediaPlayerTutorial:
  • File > New > New Project > Empty Activity
    • Name: MediaPlayerTutorial
    • Package name: org.o7planning.mediaplayertutorial
    • Language: Java
Create  raw  to contain the music file.
Prepare a music file, copy and paste to  raw folder.
If you want to play a song from a URL you must allow the application to access the network, adding the following XML code to AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET" />
Allows the app to access external storage (Such as SD Card) if you want to play media here.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.o7planning.mediaplayertutorial">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Design the interface:
If you are interested in the steps to design the interface of this application, please see the appendix at the end of the article.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="0dp"
        android:layout_height="37dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView_maxTime"
        android:layout_width="0dp"
        android:layout_height="24dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="39dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:gravity="center"
        android:text="Max Time"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/seekBar" />

    <TextView
        android:id="@+id/textView_currentPosition"
        android:layout_width="0dp"
        android:layout_height="25dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="38dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:gravity="center"
        android:text="Current Possion"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView_maxTime" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView_currentPosition">

        <Space
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/button_rewind"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="&lt;&lt;" />

        <Button
            android:id="@+id/button_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Start" />

        <Button
            android:id="@+id/button_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Pause" />

        <Button
            android:id="@+id/button_fastForward"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="&gt;&gt;" />

        <Space
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>

    <Button
        android:id="@+id/button_select"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="51dp"
        android:text="Select a Song"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package org.o7planning.mediaplayertutorial;

import androidx.appcompat.app.AppCompatActivity;

import android.media.AudioManager;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    private TextView textMaxTime;
    private TextView textCurrentPosition;
    private Button buttonPause;
    private Button buttonStart;
    private Button buttonRewind;
    private Button buttonFastForward;

    private Button buttonSelect;

    private SeekBar seekBar;
    private Handler threadHandler = new Handler();
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.textCurrentPosition = (TextView)this.findViewById(R.id.textView_currentPosition);
        this.textMaxTime=(TextView) this.findViewById(R.id.textView_maxTime);

        this.buttonSelect = (Button) this.findViewById(R.id.button_select);

        this.buttonStart= (Button) this.findViewById(R.id.button_start);
        this.buttonPause= (Button) this.findViewById(R.id.button_pause);
        this.buttonRewind= (Button) this.findViewById(R.id.button_rewind);
        this.buttonFastForward= (Button) this.findViewById(R.id.button_fastForward);

        this.buttonStart.setEnabled(false);
        this.buttonPause.setEnabled(false);
        this.buttonRewind.setEnabled(false);
        this.buttonFastForward.setEnabled(false);


        this.seekBar= (SeekBar) this.findViewById(R.id.seekBar);
        this.seekBar.setClickable(false);

        this.buttonSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Select new media source.
                selectMediaResource();
            }
        });

        this.buttonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doStart( );
            }
        });
        this.buttonPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doPause( );
            }
        });
        this.buttonRewind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doRewind( );
            }
        });
        this.buttonFastForward .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFastForward( );
            }
        });

        // Create MediaPlayer.
        this.mediaPlayer=  new MediaPlayer();
        this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        this.mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                doStop(); // Stop current media.
            }
        });
        this.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
             @Override
             public void onCompletion(MediaPlayer mp) {
                 doComplete();
             }
         });
    }

    // When user click "Select Media Source" button.
    private void selectMediaResource()  {
        // this.selectRawMediaSource();
        // this.selectURLMediaSource();
        // this.selectLocalMediaSource();
        this.selectRawMediaSource();
    }

    private void selectRawMediaSource() {
        // "mysong.mp3" ==> resName = "mysong".
        String resName = MediaPlayerUtils.RAW_MEDIA_SAMPLE;
        MediaPlayerUtils.playRawMedia(this, this.mediaPlayer, resName);
    }

    private void selectURLMediaSource()  {
        // http://example.coom/mysong.mp3
        String mediaURL = MediaPlayerUtils.URL_MEDIA_SAMPLE;
        MediaPlayerUtils.playURLMedia(this, this.mediaPlayer, mediaURL);
    }

    private void selectLocalMediaSource()  {
        // @localPath = "/storage/emulated/0/DCIM/Music/mysong.mp3"; (For example).
        String localPath = MediaPlayerUtils.LOCAL_MEDIA_SAMPLE;
        MediaPlayerUtils.playLocalMedia(this, this.mediaPlayer, localPath);
    }

    // Convert millisecond to string.
    private String millisecondsToString(int milliseconds)  {
        long minutes = TimeUnit.MILLISECONDS.toMinutes((long) milliseconds);
        long seconds =  TimeUnit.MILLISECONDS.toSeconds((long) milliseconds) ;
        return minutes + ":"+ seconds;
    }


    private void doStart( )  {
        if(this.mediaPlayer.isPlaying()) {
            return;
        }
        // The duration in milliseconds
        int duration = this.mediaPlayer.getDuration();

        int currentPosition = this.mediaPlayer.getCurrentPosition();
        if(currentPosition== 0)  {
            this.seekBar.setMax(duration);
            String maxTimeString = this.millisecondsToString(duration);
            this.textMaxTime.setText(maxTimeString);
        } else if(currentPosition== duration)  {
            // Resets the MediaPlayer to its uninitialized state.
            this.mediaPlayer.reset();
        }
        this.mediaPlayer.start();
        // Create a thread to update position of SeekBar.
        UpdateSeekBarThread updateSeekBarThread= new UpdateSeekBarThread();
        threadHandler.postDelayed(updateSeekBarThread,50);

        this.buttonPause.setEnabled(true);
        this.buttonStart.setEnabled(false);
        this.buttonRewind.setEnabled(true);
        this.buttonFastForward.setEnabled(true);
    }

    // Called by MediaPlayer.OnCompletionListener
    // When Player cocmplete
    private void doComplete()  {
        buttonStart.setEnabled(true);
        buttonPause.setEnabled(false);
        buttonRewind.setEnabled(true);
        buttonFastForward.setEnabled(false);
    }

    // Called by MediaPlayer.OnPreparedListener.
    // When user select a new media source, then stop current.
    private void doStop()  {
        if(this.mediaPlayer.isPlaying()) {
            this.mediaPlayer.stop();
        }
        buttonStart.setEnabled(true);
        buttonPause.setEnabled(false);
        buttonRewind.setEnabled(false);
        buttonFastForward.setEnabled(false);
    }

    // When user click to "Pause".
    private void doPause( )  {
        this.mediaPlayer.pause();
        this.buttonPause.setEnabled(false);
        this.buttonStart.setEnabled(true);
    }

    // When user click to "Rewind".
    private void doRewind( )  {
        int currentPosition = this.mediaPlayer.getCurrentPosition();
        int duration = this.mediaPlayer.getDuration();
        // 5 seconds.
        int SUBTRACT_TIME = 5000;

        if(currentPosition - SUBTRACT_TIME > 0 )  {
            this.mediaPlayer.seekTo(currentPosition - SUBTRACT_TIME);
        }
        this.buttonFastForward.setEnabled(true);
    }

    // When user click to "Fast-Forward".
    private void doFastForward( )  {
        int currentPosition = this.mediaPlayer.getCurrentPosition();
        int duration = this.mediaPlayer.getDuration();
        // 5 seconds.
        int ADD_TIME = 5000;

        if(currentPosition + ADD_TIME < duration)  {
            this.mediaPlayer.seekTo(currentPosition + ADD_TIME);
        }
    }


    // Thread to Update position for SeekBar.
    class UpdateSeekBarThread implements Runnable {

        public void run()  {
            int currentPosition = mediaPlayer.getCurrentPosition();
            String currentPositionStr = millisecondsToString(currentPosition);
            textCurrentPosition.setText(currentPositionStr);

            seekBar.setProgress(currentPosition);
            // Delay thread 50 milisecond.
            threadHandler.postDelayed(this, 50);
        }
    }
}
MediaPlayerUtils.java
package org.o7planning.mediaplayertutorial;


import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

public class MediaPlayerUtils {

    // "mysong.mp3" in directory "raw".
    public static final String RAW_MEDIA_SAMPLE = "mysong";

    // Example Path: /sdcard/Music/mysong.mp3
    // Example Path: /storage/emulated/0/DCIM/Music/mysong.mp3
    public static final String LOCAL_MEDIA_SAMPLE ="/sdcard/Music/mysong.mp3";
    public static final String URL_MEDIA_SAMPLE  = "https://raw.githubusercontent.com/o7planning/webexamples/master/_testdatas_/yodel.mp3";

    public static final String LOG_TAG= "MediaPlayerTutorial";

    // Play a media in directory RAW.
    // Media name = "mysong.mp3" ==> resName = "mysong".
    public static void playRawMedia(Context context, MediaPlayer mediaPlayer, String resName)  {
        try {
            // ID of video file.
            int id = MediaPlayerUtils.getRawResIdByName( context,resName);

            Uri uri = Uri.parse("android.resource://" + context.getPackageName() + "/" + id);
            Log.i(LOG_TAG, "Media URI: "+ uri);
            Toast.makeText(context,"Select source: "+ uri,Toast.LENGTH_SHORT).show();

            mediaPlayer.setDataSource(context, uri);
            mediaPlayer.prepareAsync();
        } catch (Exception e) {
            Log.e(LOG_TAG, "Error Play Raw Media: "+e.getMessage());
            Toast.makeText(context,"Error Play Raw Media: "+ e.getMessage(),Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

    // Example Path: /sdcard/Music/mysong.mp3
    // Example Path: /storage/emulated/0/DCIM/Music/mysong.mp3
    public static void playLocalMedia(Context context, MediaPlayer mediaPlayer, String localPath)  {
        try {
            Uri uri = Uri.parse("android.resource://" + localPath);
            Log.i(LOG_TAG, "Media URI: "+ uri);
            Toast.makeText(context,"Select source: "+ uri,Toast.LENGTH_SHORT).show();

            mediaPlayer.setDataSource(context, uri);
            mediaPlayer.prepareAsync();
        } catch(Exception e) {
            Log.e(LOG_TAG, "Error Play Local Media: "+ e.getMessage());
            Toast.makeText(context,"Error Play Local Media: "+ e.getMessage(),Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

    // String videoURL = "https://raw.githubusercontent.com/o7planning/webexamples/master/_testdatas_/yodel.mp3";
    public static void playURLMedia(Context context, MediaPlayer mediaPlayer, String videoURL)  {
        try {
            Log.i(LOG_TAG, "Media URL: "+ videoURL);

            Uri uri= Uri.parse( videoURL );
            Toast.makeText(context,"Select source: "+ uri,Toast.LENGTH_SHORT).show();

            mediaPlayer.setDataSource(context, uri);
            mediaPlayer.prepareAsync();

        } catch(Exception e) {
            Log.e(LOG_TAG, "Error Play URL Media: "+ e.getMessage());
            Toast.makeText(context,"Error Play URL Media: "+ e.getMessage(),Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

    // Find ID corresponding to the name of the resource (in the directory RAW).
    public static int getRawResIdByName(Context context, String resName) {
        String pkgName = context.getPackageName();
        // Return 0 if not found.
        int resID = context.getResources().getIdentifier(resName, "raw", pkgName);

        Log.i(LOG_TAG, "Res Name: " + resName + "==> Res ID = " + resID);
        return resID;
    }
}

 

3- Appendix: Interface design

Set ID, Text for components on the interface:

View more Tutorials: