11 April 2015

Add sound in Android project

What we will do ?
-> We will make 4 buttons which when clicked emits different sounds (animal sounds).

What we will need ?
  • Add buttons in activity_main.xml
  • Add sound files in resources
  • Add MediaPlayer in MainActivity.java
Step 1 : Create a New Application. Go to File->New->Android Application Project.

Step 2 : Give a name to the application (SoundDemo) and a package name(mia). Click Next continuously the click Finish.

Step 3 : Add 4 buttons to activity_main.xml from the Palette OR copy the below code in it. Give specific ids to the buttons so that you can remember it to add it in MainActivity.java.
activity_main.xml
<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"
    android:background="#585858" 
    android:padding="10dp">
    <Button
        android:id="@+id/buttonlion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:background="#D8D8D8"
        android:text="Lion" />
    <Button
        android:id="@+id/buttoncat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/buttonlion"
        android:layout_marginTop="20dp"
        android:background="#D8D8D8"
        android:text="Cat" />
    <Button
        android:id="@+id/buttonfrog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/buttoncat"
        android:layout_marginTop="20dp"
        android:text="Frog"
        android:background="#D8D8D8" />
    <Button
        android:id="@+id/buttondog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/buttonfrog"
        android:layout_marginTop="20dp"
        android:text="Dog"
        android:background="#D8D8D8" />
</RelativeLayout>


Step 4 : Download some sound files from freesound.org. You have to register and login to download files. I have downloaded the sound for lion, cat, frog and dog.

Step 5 : Add a new folder in res folder. Right-click on res->New Folder. Name it as raw. Add the sound files to it as shown below.

Step 6 : Open MainActivity.java from src->com.mia.sounddemo. Copy the code from below in it.

MainActivity.java
package com.mia.sounddemo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final Button buttonLion = (Button)findViewById(R.id.buttonlion);       
        final Button buttonCat = (Button)findViewById(R.id.buttoncat);
        final Button buttonFrog = (Button)findViewById(R.id.buttonfrog);      
        final Button buttonDog = (Button)findViewById(R.id.buttondog);
        
        final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.lion);
        final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.cat);
        final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.frog);
        final MediaPlayer mp4 = MediaPlayer.create(this, R.raw.dog);
        buttonLion.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                    mp1.start();
            }});
        buttonCat.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                    mp2.start();
            }}); 
        buttonFrog.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                    mp3.start();
            }});
        buttonDog.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                    mp4.start();
            }}); 
    }           
 }


Step 7 : Run the Project. Click on each Button to hear different sounds.

Explanation of the Code :
Explanation from activity_main.xml :
  • We have made 4 different buttons with names of 4 different animals i.e. Lion, Cat, Frog, Dog. We have given specific ids to them i.e. buttonlion, buttoncat, buttonfrog, buttondog respectively.

Explanation from MainActivity.java :
  • We have made 4 Button variables namely buttonLion, buttonCat, buttonFrog and buttonDog and connected them with buttonlion, buttoncat, buttonfrog, buttondog from activity_main.xml respectively.
final Button buttonLion = (Button)findViewById(R.id.buttonlion);      
        final Button buttonCat = (Button)findViewById(R.id.buttoncat);
        final Button buttonFrog = (Button)findViewById(R.id.buttonfrog);       
        final Button buttonDog = (Button)findViewById(R.id.buttondog);
  •   We make for MediaPlayers for 4 different sounds as mp1, mp2, mp3 and mp4 for lion,cat, frog and dog respectively. We connect them with the .wav files using create().
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.lion);
        final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.cat);
        final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.frog);
        final MediaPlayer mp4 = MediaPlayer.create(this, R.raw.dog);
buttonLion.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                    mp1.start();
            }});

Watch Video Tutorial:

Stay Tuned with Made In Android

Previous Page Next Page Home
Top