Android MediaPlayer Tutorial

Android MediaPlayer Tutorial

Howdy viewers, in this Android MediaPlayer Tutorial, you’re going to explore the most exciting programming tips. You will easily create an Android application that has the feature of Media Player (mp3 player). This simple tutorial will help you to gain the overview of the topic. Don’t worry, the code I am going to share is not too complicated, instead you can easily grab about creating a mp3 player. By the end of this tutorial, you’ll able to learn the making of Android Media Player.

Built in Method in this tutorial:

The list of JAVA methods used in this tutorial are;

  1. start();
  2. stop();
  3. pause();
  4. setDataSource();
  5. prepare();

built in Class in this tutorial:

The list of JAVA class used in this tutorial:

  1. MediaPlayer

WHY built in class and methods?




Well, again just for a recap, as I assume you’re aware of it. Android Studio has already built in class called MediaPlayer defined in .jar format and methods withing the class. So, you don’t have to write tons of code and take the pain. The only thing you’re going to do is declare the MediaPlayer object and call all the methods associated within it. (example, start(); stop(); etc.)

Bonus -Android mediaplayer tutorial:

You will love to know, you don’t need to give any special permission to the application in this tutorial, so stay away from editing AndroidManifest.xml file. But, if you’re making a really serious media player application, you might need to modify the manifest file.

Let’s start coding:

You will see two different ways of MediaPlayer tutorial, the first one is without having any controls like start, stop and pause button and the second one is with controls.

Let’s go with without controls:

  1. When the application launches, it will directly play the mp3 song. (in this tutorial, I am taking mp3 as a format). HOW? As you’re defining the start() method in the onCreate method. If you’re not aware of, when the application launches the first method Android will see for onCreate method (without any user interaction) so defining the entire coding within it makes sense.
  2. No need to edit activity-main.xml file for designing the layout.

CODES:

package com.raktim.mediaexample;
import android.app.Activity;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      MediaPlayer mediaPlayer=new MediaPlayer();
      try{
       mediaPlayer.setDataSource("/storage/sdcard0/download/test.mp3");
            mediaPlayer.prepare();
            mediaPlayer.start();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }


    }
================
 Explanation:
================
1. As you know, when any Android application launches, it will go to onCreate method and check for codes to execute. In this case, it will directly play the mp3 player without any user interaction to control.
2. Within the onCreate method, we call the MediaPlayer object from the MediaPlayer class. The purpose, to get use of all the methods defined in the class. Code is MediaPlayer mediaPlayer=new MediaPlayer();
3. Once MediaPlayer object created, it will store in mediaPlayer so that we can call the methods using mediaPlayer.
4. Next, we set the source for the mp3 file, mediaPlayer.setDataSource("/storage/sdcard0/download/test.mp3"); In this case, the mp3 file path to my external storage is in the download folder.
5. After we set the path, we need to prepare the path for playing. Use the code mediaPlayer.prepare();
6. As we have prepared the mp3 path to play, we need to start the playing. This can be achieved by calling the start(); method. Use the code mediaPlayer.start();
NOTE: You need to put the entire coding from setting path to start in try{..} catch(Exception e){..}

Without controls -OUTPUT SCREEN

MediaPlayer without controls

Let’s see with controls:

You have to modify the activity_main.xml file to set the layout and to add start, stop and pause buttons. In this tutorial, I am using LinearLayout. I gave the button’s id as start, stop and pause to start, stop and pause the music of the mp3 respectively. The design is easy, I will not go in depth. If you have doubts, comment below.

DESIGN CODE:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.raktim.mediaexample.MainActivity">

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

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop"
        tools:layout_editor_absoluteX="115dp"
        tools:layout_editor_absoluteY="0dp" />
    <Button
        android:id="@+id/pause"
        android:text="Pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

package com.raktim.mediaexample;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
Button start,stop,pause;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     final MediaPlayer mediaPlayer=new MediaPlayer();
        start=(Button)findViewById(R.id.start);
        stop=(Button)findViewById(R.id.stop);
        pause=(Button)findViewById(R.id.pause);
        try{
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getPath() +"/download/test.mp3");
            mediaPlayer.prepare();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.start();
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.stop();
            }
        });
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.pause();
            }
        });
    }
    }
=============
Explanation:
=============
The explanation is same as without controls. The only thing we did is declaring the MediaPlayer object as final and added setOnClickListener to all the buttons. I will leave it to you to study and understand it. If you have any doubt, drop comments, I will get back to you as soon as possible.

with controls -output screens

MediaPlayer with controls

Conclusion:

I guess that’s it. You by now can easily create an MediaPlayer android application. If you’ve doubts drop comments below and I will get back to you as soon as possible.

Leave a Reply

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