Playing audio in Android

  • In the current era, most of the people like music and these people purchase the multimedia supported phone to enjoy the music and movies.
  • For this purpose, android provides the inbuilt media player and it is easy to use.
  • We can use the music in our application by just follow the three steps as follows:
    • First of all we have to copy the mp3 file in to the res/raw folder.
    • After that we have to create the object of the MediaPlayer class and we have to pass the reference of mp3 file, and after that we will call the MediaPlayer.create() method and inside this method we have to pass the current activity context and the URL of the mp3 file.
    • Now we have to call the start() method with the help of MediaPlayer’s object.
    • The basic syntax to include the mp3 file in our application as follows:
    • MediaPlayer mp = new MediaPlayer();
      MediaPlayer mp1;
      mp1= mp.create(Context context, mp3 file id);
    • We can use this syntax also as follows:
    • MediaPlayer mp1;
      mp1= MediaPlayer.create(Context context, mp3 file id);
    • Always use the MediaPlayer.create() method because this is static method but we can access this method by object of the MediaPlayer class.
  • Here is the sample example of the multimedia and the xml file is as follows:
  • <RelativeLayout xmlns:android=""
    tools:context=".MainActivity" >

    android:text="Play sound in android example" />
    android:text="Play Sound" />
  • Here we have used the relative layout and used a text view to display the message and used a button on which we have to perform the action.
  • The java file is as follows
  • package com.example.androidmultimedia;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class MainActivity extends Activity {
    Button play;

    protected void onCreate(Bundle savedInstanceState) {

    play = (Button) findViewById(;

    play.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
    // TODO Auto-generated method stub

    public void playSound(View view)

    MediaPlayer mp1;
    mp1 = MediaPlayer.create(this, R.raw.ans1);

    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(, menu);
    return true;
  • In the java file first we have imported packages which are as follows:
  • android
  • Here we have used the which provides the method related to the multimedia.
  • After that we have declared the following variables:
  • android
  • Here we have declared a button type variable, now we have to register this variable in the onCreate() method as follows:
  • android
  • Here we have registered the button using the id provided in the xml layout file.
  • After that we have used the onClickListener() method to perform some action when we click on the button. Here inside the onClick() method, we have to call the playSound() method that takes view type argument as follows:
  • android
  • Inside this method, first of all we have declared an mp1 variable of MediaPlayer type. After that we have assigned the MediaPlayer.create() method that will take two arguments; first argument is used for current context and 2nd argument is used for the url of the sound file.
  • After that we have to call the start() method using the object of MediaPlayer class.
  • The output is as follows:
  • android
  • When we will click on the button then the music will play in the background.