VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-build-a-simple-music-player-app-using-android-studio/

⇱ How to Build a Simple Music Player App Using Android Studio - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Build a Simple Music Player App Using Android Studio

Last Updated : 12 Jul, 2025

This is a very simple app suitable for beginners to learn the concepts. 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.
👁 How-to-Build-a--Simple-Music--Player-App_


Step By Step Implementation

Step 1: Create a New Android Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Note: Select Java as the programming language.

Step 2: Designing the User Interface of the app

In this app, we have used 4 components:

  • ImageView - to display the album cover of the song
  • 3 ImageViews (used as buttons) :
    • a play button to play our song
    • a pause button to pause our song
    • a stop button to stop our song
  • A SeekBar - to keep track of the progress of the music
  • 2 TextViews
    • to show current time
    • to show total time of the 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

activity_main.xml


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 > Android Resource Directory

Name the newly created directory as raw and add all the audio files in this folder. Make sure that the new name contains all small alphabets. The only valid characters are (a-z and 0-9 and _ )

Step 4: Adding drawable

Navigate to app > res > drawable, right click on the folder and choose New > Drawable Resource File and create 3 such files and name them play.xml, pause.xml and stop.xml


Step 5: 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);


MainActivity.java:


Output:


Comment

Explore