Add Sound to Your Application in Android Studio

Introduction

 
This article explains how to start sound on a button click using the raw folder in Android Studio.
 
In this first, you will use an Imageview and two buttons. When you click on the Start button the sound will start and when you click on the Stop button the sound will stop. In this I use a Dog as an example, you can use any other animal as an example.
 
So to apply a sound in your application you need to create a file named raw in the res folder and add the sound file with its extension (Dog.mp3 or any other extension) inside the file. you will create the folder as in the following:
 
Clipboard02.jpg
 
So after adding the sound file to the raw folder, you will create a media player in your class file. You will add a media player and a sound to the media player like this:
MediaPlayer player=MediaPlayer.create(MainActivity.this,R.raw.dog);
 
You will do the start sound operation on a Start button click event and do the stop operation on the Stop button click event like this:
  1. button1.setOnClickListener(new View.OnClickListener() {  
  2.    
  3.             @Override  
  4.             public void onClick(View v) {  
  5.                 player=MediaPlayer.create(MainActivity.this,R.raw.dog);  
  6.    
  7.            player.start();  
  8.    
  9.             }  
  10.         });  
  11.         button2.setOnClickListener(new View.OnClickListener() {  
  12.             @Override  
  13.             public void onClick(View v) {  
  14.                 player.pause();  
  15.    
  16.             }  
  17.    
  18.     }); 
Step 1
 
Create the project like this:
 
Clipboard03.jpg
 
Step 2
 
Create an XML file and write this:
 
In this, I have used a Textview and two Buttons. An Imageview inside the Relative layout. When you click on the Start button the sound will start and when you click on the Stop button the sound will stop. In this I use the example of a Dog, you can use any animal as an example.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity"  
  10.         android:background="#566780">  
  11.    
  12.     <TextView  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="SoundComesonImageClick"  
  16.         android:layout_centerHorizontal="true"  
  17.         android:textStyle="bold"  
  18.             android:textSize="20dp"/>  
  19.    
  20.     <ImageView  
  21.             android:layout_height="wrap_content"  
  22.             android:layout_width="wrap_content"  
  23.             android:background="@drawable/images"  
  24.             android:layout_centerHorizontal="true"  
  25.             android:layout_centerInParent="true"  
  26.             android:id="@+id/imageview">  
  27.    
  28.             </ImageView>  
  29.    
  30.     <Button  
  31.             android:id="@+id/button1"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_width="100dp"  
  34.             android:layout_alignParentBottom="true"  
  35.             android:text="Start"  
  36.             android:layout_marginLeft="50dp"  
  37.             />  
  38.     <Button  
  39.             android:id="@+id/button2"  
  40.             android:layout_height="wrap_content"  
  41.             android:layout_width="100dp"  
  42.             android:layout_alignParentBottom="true"  
  43.             android:layout_marginLeft="200dp"  
  44.             android:text="Stop"  
  45.             >  
  46.     </Button>  
  47. </RelativeLayout> 
Step 3
 
In a Java class file, you will add a media player and sound to the media player. On the start button click event, the sound will start and on the stop button click event the sound will stop.
 
Create a Java file and write this:
  1. package com.addmusic;  
  2. import android.media.MediaPlayer;  
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.ImageView;  
  9.    
  10. public class MainActivity extends Activity {  
  11.     MediaPlayer player;  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.         ImageView imageview=(ImageView)findViewById(R.id.imageview);  
  17.         Button button1=(Button)findViewById(R.id.button1);  
  18.         Button button2=(Button)findViewById(R.id.button2);  
  19.        button1.setOnClickListener(new View.OnClickListener() {  
  20.             @Override  
  21.             public void onClick(View v) {  
  22.                 player=MediaPlayer.create(MainActivity.this,R.raw.dog);  
  23.    
  24.            player.start();  
  25.    
  26.             }  
  27.         });  
  28.         button2.setOnClickListener(new View.OnClickListener() {  
  29.             @Override  
  30.             public void onClick(View v) {  
  31.                 player.pause();  
  32.    
  33.             }  
  34.    
  35.     });  
  36.    
  37.     }  
  38.    

Step 4
 
Android menifest.xml file:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.addmusic"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.addmusic.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.    
  27. </manifest> 
Step 5
 
Clipboard04.jpg


Similar Articles