Download Code

Android Media Player Example

In android media player example you will learn how to develop basic Audio/Media Player Application in android.Basically in this tutorials we’ll talk about MediaPlayer and it’s controls.

Media Player

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

Let’s start to create Media Player (Audio) first go ahead and create android media player example application project.

media player,android media player,android media player example,media player example,

after you created android media player 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 for Media Player.

activity_main.xml

media player,android media player,android media player example,media player example,

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

Now go to MainActivity.java class file.First time your code should be like this.

public class MainActivity extends Activity {
              @SuppressLint("NewApi") @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    // setting xml layout
        }
}

Now First step is to refer your UI Component in MainActivity.java class.So for that declare variables for every button like this.

public class MainActivity extends Activity {
        // button variable next,previous,play,exit
	Button next,prev,play,exit;
        @SuppressLint("NewApi") @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    // setting xml layout
        }
}

next create a public method within your parent class where you will refer all button id’s and call that method in onCreate method.like this.

public class MainActivity extends Activity {
        // button variable next,previous,play,exit
	Button next,prev,play,exit;
        @SuppressLint("NewApi") @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
        }
 // 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);
    }
}

then next step is to create a folder inside your res

directory in particular project explorer panal.Just right click on res folder > New > Folder > Name that folder as raw.Now place songs in that raw folder.After placing that songs it will look this.

media player,android media player,android media player example,media player example,

Now we will initialize an integer array and store songs path from Resource in an integer array using public method.Then call that method in onCreate method.

public class MainActivity extends Activity {
               // button variable next,previous,play,exit
               Button next,prev,play,exit;
               // integer array for songs
               Integer songs[] = new Integer[3];
               @SuppressLint("NewApi") @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
               }
              // 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;
             }
}

Now create a Media Player class variable and call Media Player create method and pass it two parameters.

  • Context
  • Integer Resource

like this.

public class MainActivity extends Activity {
               // button variable next,previous,play,exit
               Button next,prev,play,exit;
               // Media Player class variable 
	       MediaPlayer song;
               // integer array for songs
               Integer songs[] = new Integer[3];
               @SuppressLint("NewApi") @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]);
               }
              // 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;
             }
}

Now next you will apply Click event’s on Play,Next and Previous button’s for Media Player controls to be used.If you are not familiar with click event’s go through this Tutorial.i have added every line comment.After doing that your final code will look like this.

public class MainActivity extends Activity {

	// 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;
	
    @SuppressLint("NewApi") @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 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 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 OnClickListener() {
			
			@SuppressLint("NewApi") @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;
    }
    
}

Run your android media player example project output.
media player,android media player,android media player example,media player example,

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