Playing audio in an Android application


          It is really simple to use audio files in your application. Just like the graphics you use on the display to convey some information to the user, you can use audio to convey information to the user in your application.Android supports music and sound through MediaPlayer class. This class is available in android.media package.
setVolumeControlStream() method is used to tell the Android that the user presses the volume up or down keys while this application is running. This will adjust the volume of the audio file being played instead of the ringer volume.

We need to copy the audio files to /res/raw directory.
To know the use of various android directories visit this post.
Copying a file into the res directory causes the Android eclipse IDE to define a Java identification for you in the R.java class. So we can simply access the audio file using R.raw.filename

Once you are done with this, all you need to do is create a MediaPlayer instance and call start() method.
MediaPlayer mp=MediaPlayer.create(this, resId);
mp.start();
here this is the current context and resId is the id of the media resource.

Once we finish the audio play we have to release the resources assigned to the media player. Too many unreleased resources can cause exceptions in the application. This can be done in onDestroy() method.

Sample Application:
In this sample application we have copied two audio files a.mp3 and b.mp3 to /res/raw directory. We have added two buttons in the layout xml file to play each of the audio files.

Application structure:



Layout xml file:

<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"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="22dp"
        android:layout_marginTop="36dp"
        android:text="Play audio a.mp3" />

    <Button
        android:id="@+id/button_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button_1"
        android:layout_below="@+id/button_1"
        android:text="Play audio b.mp3" />

</RelativeLayout>


Activity class:

package com.example.playaudio;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * @author Prabu
 *
 */
public class MainActivity extends Activity implements OnClickListener{
 private MediaPlayer mp;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  setVolumeControlStream(AudioManager.STREAM_MUSIC);
  Button button1=(Button)findViewById(R.id.button_1);
  Button button2=(Button)findViewById(R.id.button_2);
  button1.setOnClickListener(this);
  button2.setOnClickListener(this);
 }

 public void onClick(View v) {
  int resId;
  switch (v.getId()) {
  case R.id.button_1:
    resId = R.raw.a;
    break;
  case R.id.button_2:
     resId = R.raw.b;
    break;
  default:
    resId = R.raw.a;
    break;
  }
  // Release any resources from previous MediaPlayer
  if (mp != null) {
     mp.release();
  }
  // Create a new MediaPlayer to play this sound
  mp = MediaPlayer.create(this, resId);
  mp.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.activity_main, menu);
   return true;
 }
 
 @Override
 protected void onDestroy() {
   if(null!=mp){
  mp.release();
   }
   super.onDestroy();
 }
}


Output screenshot:




Source code of the application:
PlayAudio.zip

 



Reactions:

4 comments :

  1. It did not work..
    Error popup is coming.
    Unfortunatly. PlayAudio stopped.

    ReplyDelete
  2. It worked for me....really helpful for me....
    thank you :)

    ReplyDelete
  3. How to autoplay 1st track & after it stop or finish start next track...?

    ReplyDelete