Download Code

Android Studio MediaPlayer Example

In android studio mediaplayer example you will learn how to develop basic Audio/Media Player Application in android.

Basically in this tutorial we’ll talk about Media Player and it’s controls.

MediaPlayer

Media Player class used to play audio and video files or streams.There are lot of states of Media Player class for audio and video files/streams.You can get further detail in above Media Player hyper link.

You have some background of MediaPlayer class.Furthermore let’s create new android studio mediaplayer example application project.

First we play with XML file to design the layout for Media Player.I have created just three buttons for Media Player.

  • Next
  • Previous
  • Play

here is XML code with design preview for Media Player.

activity_main.xml

android studio mediaplayer,android studio mediaplayer example,mediaplayer example,mediaplayer,

alright.Next you will write some code to interact with Media Player (Audio) UI.

Now go to MainActivity.java class file.Do the following steps

  • Create variables for UI Components and refer their id’s.
  • Create a folder inside your res directory called raw.
  • Now place songs in that raw folder(copy & paste).
  • Initialize an integer array and store songs path from Resource.
  • Create a Media Player class variable,call  method and pass it two parameters(Context,Integer Resource).
  • Apply Click event’s on button’s for Media Player control.If you are not familiar with click event’s go through this Tutorial.

MainActivity.java

package com.example.anonymous.studiomediaplayer;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    // button variable next,previous,stop,exit
    Button next,prev,play,exit;
    // Media Player class variable
    MediaPlayer song;
    // integer array for songs
    Integer songs[] = new Integer[3];
    //currently running position variable
    public static int position = 0;

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    // setting xml layout
        initialize();        // public method to get all id's of buttons
        load_song();         // calling songs in array method

        // create is public method of MediaPlayer class accepting two parameters Context and reference to that song
        song = MediaPlayer.create(getApplicationContext(), songs[position]);

        // next button click event
        next.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // condition to check whether song is playing or not
                // isPlaying is a method to check
                if(song.isPlaying()){
                    song.stop(); // method to stop the object
                }
                // condition to start first if continuously pressing next button
                if(position==2)
                {
                    position=0;  // setting to zero to first song
                }
                else{
                    position++;     // increment operator if press next
                }
                // create is public method of MediaPlayer class accepting two parameters Context and reference to next song
                song = MediaPlayer.create(getApplicationContext(), songs[position]);
                song.start();   // to start song
            }
        });

        // previous button click event

        prev.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // condition to check whether song is playing or not
                // isPlaying is a method to check
                if(song.isPlaying()){
                    song.stop(); // method to stop the object
                }
                // condition to start first if continuously pressing prev button
                if(position==0){
                    position=0;  // setting to zero to first song
                }
                else{
                    position--; //decrement operator if press prev
                }
                // create is public method of MediaPlayer class accepting two parameters Context and reference to previous song
                song = MediaPlayer.create(getApplicationContext(), songs[position]);
                song.start(); // to start song
            }
        });
        // play button click event
        play.setOnClickListener(new View.OnClickListener() {

           @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                song.start();  // to start first song
            }
        });

    }
    // this public method is used here to get all UI Component id's
    public void initialize(){
        next = (Button) findViewById(R.id.button1);
        prev = (Button) findViewById(R.id.button2);
        play = (Button) findViewById(R.id.button3);


    }
    // this public method is used to store songs in an integer array
    public void load_song(){
        songs[0] = R.raw.drift;
        songs[1] = R.raw.shakira;
        songs[2] = R.raw.main;
    }

}

Compile and Run your android studio mediaplayer example application project.

android studio mediaplayer,android studio mediaplayer example,mediaplayer example,mediaplayer,

that’s it.I hope you like this android studio mediaplayer example tutorial.
Don’t forget to give your feedback.
Thanks.