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);
      mp1.start();
    • We can use this syntax also as follows:
    • MediaPlayer mp1;
      mp1= MediaPlayer.create(Context context, mp3 file id);
      mp1.start();
    • 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="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Play sound in android example" />
    <Button
    android:id="@+id/btnPlay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginRight="34dp"
    android:layout_marginTop="136dp"
    android:text="Play Sound" />
    </RelativeLayout>
  • 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.media.MediaPlayer;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class MainActivity extends Activity {
    Button play;

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

    play = (Button) findViewById(R.id.btnPlay);

    play.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    playSound(arg0);
    }
    });
    }

    public void playSound(View view)
    {

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

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }
    }
  • In the java file first we have imported packages which are as follows:
  • android
  • Here we have used the android.media.MediaPlayer 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.