Open In App
Related Articles

How to build a simple music player app using Android Studio

Like Article
Save Article
Report issue

This is a very simple app suitable for beginners to learn the concepts. The following


is good enough before starting.

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, audios, etc) in our project.
  • Building the interface of our Music Player Android App.

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


and give your project a unique name as you wish but don’t forget to keep the first alphabet capital.

  1. Go to the top left corner and then hit File->New->New Project as shown in the following screenshot.
  2. Select Empty Activity as shown in the following screenshot.
  3. Give your project a name, choose java and use lower level API so that your app can run on older version of android phones(I am using Api 16: Android 4.1 Jelly Bean). Step 2: Designing the User Interface of the app In this app, we have used 4 components:
    • a 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:
    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"?>
    <LinearLayout xmlns:android=""
            android:contentDescription="@string/todo" />
                android:onClick="musicpause" />
                android:onClick="musicplay" />
                android:onClick="musicstop" />
    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
    1. 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
    2. We will add our music file to this newly created object by using create function :
      music = MediaPlayer.create(this, R.raw.sound);
      Please 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)
    3. 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(); }
    4. For pause button we will use the inbuilt function pause. This will pause the song.
      public void pauseSong(View v) { mp.pause(); }
    5. 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
    6. The complete Java code: file
    7. Java

      package com.example.amusinz;
      import android.os.Bundle;
      import android.view.View;
      public class MainActivity
          extends AppCompatActivity {
          // Instantiating the MediaPlayer class
          MediaPlayer music;
          protected void onCreate(
              Bundle savedInstanceState)
              // 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)
          // Pausing the music
          public void musicpause(View v)
          // Stopping the music
          public void musicstop(View v)
                  = MediaPlayer.create(
                      this, R.raw.sound);
    8. Step 5: Let’s Run our app
    9. Click the “
    10. Run
    11. ” button at the Toolbar at the top to run our code. You can run your app two ways:
      • using Android Virtual Device (emulator)
      • by connecting your phone using USB
    12. You must enable the developer options in your phone and set the USB debugging mode on to run your app.
    13. Please 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.

Last Updated : 01 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads