How to Build a Simple Music Player App Using Android Studio
This is a very simple app suitable for beginners to learn the concepts. The following prerequisites are good enough before starting.
- Android fundamentals for beginners
- Install and Set up Android Studio
- First Android App Project
- How to Run your first Android App
The following things you will learn in this article:
- Implementing MediaPlayer class and using its methods like pause, play and stop.
- Using external files ( images, audio, etc ) in our project.
- Building the interface of our Music Player Android App.
Step By Step Implementation to Play a Music in Android – Java
After finishing all the following subsequent steps our app will look like this:
Step 1: Open a New Android Project
After opening the Android Studio you have to create a new project using the Empty Activity with language as Java and give your project a unique name as you wish but don’t forget to keep the first alphabet capital.
Step 2: Designing the User Interface of the app
In this app, we have used 4 components:
- An ImageView- to show our given image for the song
- 3 Buttons:
- a play button to play our song
- a pause button to pause our song
- a stop button to stop our song
Note: if we press play after pressing the pause then our song will continue playing immediately after where it was paused but if we press play button after stop then our song will play from the beginning
These components are implemented on the below two layouts:
- Vertical LinearLayout
- Horizontal LinearLayout
Inside the LinearLayout (vertical) there are two components:
- ImageView component
- LinearLayout(horizontal)
This layout will vertically divide our app screen in two halves. The
imageView
component will be on upper half and the
Horizontal Linear Layout
will be on the lower half. The horizontal layout will contain three buttons (play, pause and stop button). This horizontal layout will align these three buttons one after another horizontally on the lower half of our app screen.
To understand this clearly, please go through the following blue print and Component Tree of our app:
In our app I have used different styles for play, pause and stop button by adding the following line of code:
android:background=”@android:drawable/ic_media_play” for play button android:background=”@android:drawable/ic_media_pause” for pause button android:background=”@android:drawable/ic_delete” for stop button
Here geeksforgeeks logo is used in the app. Select the image and then paste it in the drawable directory. The path of the directory:
project->app->src->main->res->drawable
Set the id of all components and add the onClick methods with the buttons.
Below is the XML code for the activity_main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Music Player Android"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/gfg_logo"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
<ImageButton
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pause"
android:clickable="true"
android:contentDescription="Pause Button"
android:onClick="musicpause"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<ImageButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/play_arrow"
android:clickable="true"
android:contentDescription="Play Button"
android:onClick="musicplay"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<ImageButton
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/stop"
color
android:clickable="true"
android:contentDescription="Stop Button"
android:onClick="musicstop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Adding the music file to our app
Add the mp3 file to the raw folder. You can reach there by:
app-> res-> raw
If there is no raw folder, then create it by right-clicking on res directory then:
res-> new-> directory
Name the newly created directory as raw and add all the audio files in this folder. Drag and drop files here is not allowed. You have to copy your source file, then right-click on the raw directory and click paste. Use “show in explorer” (if you are using windows) to go to that particular file. Make sure that the new name contains all small alphabets. The only valid characters are (a-z and 0-9 and _ )
Step 4: Let’s code the functionality of our App
Make a object of MediaPlayer class named music. It is an inbuilt class in android package. All the properties of the MediaPlayer class can be used by this music object:
MediaPlayer music
We will add our music file to this newly created object by using create function :
music = MediaPlayer.create(this, R.raw.sound);
Note: that there is no need to add .mp3 or .wav or whatever filetype you are using. Just add the name of the file. (I have named my file as sound.mp3 so used R.raw.sound)
MediaPlayer class has an inbuilt function called start we will use this function for play button. It will start the song.
public void playSong(View v){
music.start();
}
For pause button we will use the inbuilt function pause. This will pause the song.
public void pauseSong(View v) {
mp.pause(); }
For stop button we will use the inbuilt stop function. This function also deletes the object (music), so we create a new object with the same name.
public void stopSong(View v) {
mp.stop();
}
music = MediaPlayer.create(this, R.raw.sound);
Note: The audio files are stored in the app, so make sure small files are added
The complete Java code: MainActivity.java file
package com.gfg.music_player_java;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// Instantiating the MediaPlayer class
MediaPlayer music;
@Override
protected void onCreate(
Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Adding the music file to our
// newly created object music
music = MediaPlayer.create(this, R.raw.sound);
}
// Playing the music
public void musicplay(View v)
{
music.start();
}
// Pausing the music
public void musicpause(View v)
{
music.pause();
}
// Stopping the music
public void musicstop(View v)
{
music.stop();
music = MediaPlayer.create(this, R.raw.sound);
}
}
To Access the code of the whole application : Click here
Step 5: Let’s Run our app
Click the “Run” button at the Toolbar at the top to run our code.
You can run your App Two ways:
- Using Android Virtual Device (emulator)
- Or by connecting your phone using USB
You must enable the developer options in your phone and set the USB debugging mode on to run your app.
Note: that the emulator consumes a lot of RAM so your system should have enough ram to launch the emulator, it is recommended to use 4gb or more ram to increase the performance of your emulator.