Adding Audio to an Android Application

Introduction 

 
Adding audio to your android application is really not a hard task. All we need is to add your audio content into your Android Studio Project and try to play and pause the audio you have added. In this article, I will demonstrate adding audio into your Android Application and how to control it.
 

Steps to add your audio to your Android Studio project

 
Step 1
 
Open your Android Studio and create a new project.
 
Adding Audio To An Android Application 
 
Step 2
 
Create a new folder named "raw" in your Android project's "res" folder and place your audio file inside the "raw" folder. 
 
Adding Audio To An Android Application 
Adding Audio To An Android Application
 
You can see the audio is now successfully added into your Android Studio project by viewing the "raw" subfolder under "res" folder.
 
Adding Audio To An Android Application
 
 
There is no visual component to interact with the audio that already comes within the Android Studio. The audio added will run in the background.
 

Steps to play and pause audio

 
Step 1
 
Set up the MediaPlayer class in onCreate() method of your Java code.
  1. MediaPlayer mediaPlayer = MediaPlayer.create(context: this, R.raw.your_audioname );  
Android is providing MediaPlayer class to access built-in mediaplayer services like playing audio, video etc,. 
 
Step 2
 
Since there is no visual component to control the audio, I am adding two buttons with id 'play' and 'pause' for the purpose of playing the audio and pausing it respectively.
 
And also I have set up the onClickListener for both the buttons. 
 
Step 3
 
To start the audio, use the start() method of MediaPlayer Class 
  1. mediaPlayer.start();  
Step 4
 
To pause the audio, use the pause() method of MediaPlayer Class 
  1. mediaPlayer.pause();  
For reference, here I provide the source code of my Android project.
 
activity_main.xml 
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"    
  4.     xmlns:tools="http://schemas.android.com/tools"    
  5.     android:layout_width="match_parent"    
  6.     android:layout_height="match_parent"    
  7.     tools:context=".MainActivity">    
  8.     
  9.     <Button    
  10.         android:id="@+id/play"    
  11.         android:layout_width="wrap_content"    
  12.         android:layout_height="wrap_content"    
  13.         android:layout_marginStart="160dp"    
  14.         android:layout_marginTop="232dp"    
  15.         android:text="play"    
  16.         app:layout_constraintBottom_toTopOf="@+id/pause"    
  17.         app:layout_constraintEnd_toEndOf="parent"    
  18.         app:layout_constraintHorizontal_bias="0.006"    
  19.         app:layout_constraintStart_toStartOf="parent"    
  20.         app:layout_constraintTop_toTopOf="parent"    
  21.         app:layout_constraintVertical_bias="0.578" />    
  22.     
  23.     <Button    
  24.         android:id="@+id/pause"    
  25.         android:layout_width="wrap_content"    
  26.         android:layout_height="wrap_content"    
  27.         android:layout_marginBottom="324dp"    
  28.         android:text="pause"    
  29.         app:layout_constraintBottom_toBottomOf="parent"    
  30.         app:layout_constraintEnd_toEndOf="parent"    
  31.         app:layout_constraintHorizontal_bias="0.498"    
  32.         app:layout_constraintStart_toStartOf="parent" />    
  33. </androidx.constraintlayout.widget.ConstraintLayout>    
MainActivity.java
  1. package com.thetechiechum.audiodemo;  
  2.   
  3. import androidx.appcompat.app.AppCompatActivity;  
  4.   
  5. import android.media.MediaPlayer;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.   
  12.     MediaPlayer mediaPlayer;  
  13.     Button play,pause;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.   
  20.         mediaPlayer = MediaPlayer.create(this, R.raw.audioclip);  
  21.   
  22.         play = (Button)findViewById(R.id.play);  
  23.         pause =(Button) findViewById(R.id.pause);  
  24.   
  25.         play.setOnClickListener(new View.OnClickListener() {  
  26.             @Override  
  27.             public void onClick(View view) {  
  28.                 mediaPlayer.start();  
  29.             }  
  30.         });  
  31.   
  32.         pause.setOnClickListener(new View.OnClickListener() {  
  33.             @Override  
  34.             public void onClick(View view) {  
  35.                 mediaPlayer.pause();  
  36.             }  
  37.         });  
  38.   
  39.     }  
  40. }  
In this article, we have seen the steps to add audio to your Android application and the ways to control the audio. In the next article, I will demonstrate how to adjust the volume of the audio. 


Similar Articles