Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
How to Build a Simple Music Player App Using Android Studio
Next article icon

How to Build a Simple Music Player App using Android Kotlin

Last Updated : 23 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

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 Kotlin 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

activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout      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"     android:background="@color/white"     android:gravity="center"     android:orientation="vertical"     android:padding="16dp"     tools:context=".MainActivity">      <ImageView         android:id="@+id/imageView"         android:layout_width="250dp"         android:layout_height="250dp"         android:scaleType="centerCrop"         android:src="@drawable/album_cover" />      <SeekBar         android:id="@+id/seekBar"         android:layout_width="280dp"         android:layout_height="wrap_content"         android:layout_marginTop="24dp"         android:layout_marginBottom="8dp"         android:progress="33"         android:progressTint="@color/darker_grey"         android:thumbTint="@color/darker_grey" />      <LinearLayout         android:layout_width="250dp"         android:layout_height="wrap_content"         android:orientation="horizontal"         android:weightSum="2">          <TextView             android:id="@+id/textCurrentTime"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_weight="1"             android:gravity="start"             android:text="0:00" />          <TextView             android:id="@+id/textTotalTime"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_weight="1"             android:gravity="end"             android:text="0:00" />     </LinearLayout>      <LinearLayout         android:layout_width="250dp"         android:layout_height="wrap_content"         android:layout_marginTop="32dp"         android:orientation="horizontal"         android:weightSum="3">          <ImageView             android:id="@+id/buttonPause"             android:layout_width="0dp"             android:layout_height="40dp"             android:layout_weight="1"             android:clickable="true"             android:src="@drawable/pause"             app:tint="@color/darker_grey" />          <ImageView             android:id="@+id/buttonPlay"             android:layout_width="0dp"             android:layout_height="40dp"             android:layout_weight="1"             android:src="@drawable/play"             app:tint="@color/darker_grey" />          <ImageView             android:id="@+id/buttonStop"             android:layout_width="0dp"             android:layout_height="40dp"             android:layout_weight="1"             android:src="@drawable/stop"             app:tint="@color/darker_grey" />     </LinearLayout> </LinearLayout> 


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

play.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"     android:width="24dp"     android:height="24dp"     android:viewportWidth="960"     android:viewportHeight="960">   <path       android:pathData="M320,760v-560l440,280 -440,280ZM400,480ZM400,614 L610,480 400,346v268Z"       android:fillColor="#e8eaed"/> </vector> 
pause.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"     android:width="24dp"     android:height="24dp"     android:viewportWidth="960"     android:viewportHeight="960">   <path       android:pathData="M520,760v-560h240v560L520,760ZM200,760v-560h240v560L200,760ZM600,680h80v-400h-80v400ZM280,680h80v-400h-80v400ZM280,280v400,-400ZM600,280v400,-400Z"       android:fillColor="#e8eaed"/> </vector> 
stop.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"     android:width="24dp"     android:height="24dp"     android:viewportWidth="960"     android:viewportHeight="960">   <path       android:pathData="M320,320v320,-320ZM240,720v-480h480v480L240,720ZM320,640h320v-320L320,320v320Z"       android:fillColor="#e8eaed"/> </vector> 


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);


MainActivity.kt:

MainActivity.java
package org.geeksforgeeks.demo  import android.media.MediaPlayer import android.os.Bundle import android.os.Handler import android.os.Looper import android.widget.ImageView import android.widget.SeekBar import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import java.util.concurrent.TimeUnit  class MainActivity : AppCompatActivity() {      // Declare MediaPlayer for audio playback     private lateinit var mediaPlayer: MediaPlayer      // Declare UI elements     private lateinit var seekBar: SeekBar     private lateinit var textCurrentTime: TextView     private lateinit var textTotalTime: TextView     private lateinit var buttonPlay: ImageView     private lateinit var buttonPause: ImageView     private lateinit var buttonStop: ImageView      // Handler to update SeekBar and current time text every second     private val handler = Handler(Looper.getMainLooper())      // Runnable task that updates SeekBar and current playback time     private val updateSeekBar: Runnable = object : Runnable {         override fun run() {             if (::mediaPlayer.isInitialized && mediaPlayer.isPlaying) {                                  // Update SeekBar progress and current time text                 seekBar.progress = mediaPlayer.currentPosition                 textCurrentTime.text = formatTime(mediaPlayer.currentPosition)                  // Repeat this task every 1 second                 handler.postDelayed(this, 1000)             }         }     }      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          // Initialize views from layout         seekBar = findViewById(R.id.seekBar)         textCurrentTime = findViewById(R.id.textCurrentTime)         textTotalTime = findViewById(R.id.textTotalTime)         buttonPlay = findViewById(R.id.buttonPlay)         buttonPause = findViewById(R.id.buttonPause)         buttonStop = findViewById(R.id.buttonStop)          // Create MediaPlayer instance with a raw audio resource         mediaPlayer = MediaPlayer.create(this, R.raw.sound)          // Set listener to configure SeekBar and total time after MediaPlayer is ready         mediaPlayer.setOnPreparedListener {             seekBar.max = it.duration             textTotalTime.text = formatTime(it.duration)         }          // Play button starts the audio and begins updating UI         buttonPlay.setOnClickListener {             mediaPlayer.start()             handler.post(updateSeekBar)         }          // Pause button pauses the audio playback         buttonPause.setOnClickListener {             mediaPlayer.pause()         }          // Stop button stops playback and resets UI and MediaPlayer         buttonStop.setOnClickListener {             mediaPlayer.stop()             mediaPlayer = MediaPlayer.create(this, R.raw.sound)             seekBar.progress = 0             textCurrentTime.text = "0:00"             textTotalTime.text = formatTime(mediaPlayer.duration)         }          // Listen for SeekBar user interaction         seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {              // Called when progress is changed             override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {                 if (fromUser) {                     // Seek MediaPlayer to new position and update current time                     mediaPlayer.seekTo(progress)                     textCurrentTime.text = formatTime(progress)                 }             }              // Not used, but required to override             override fun onStartTrackingTouch(seekBar: SeekBar?) {}              // Not used, but required to override             override fun onStopTrackingTouch(seekBar: SeekBar?) {}         })     }      // Format milliseconds into minutes:seconds format (e.g., 1:05)     private fun formatTime(milliseconds: Int): String {         val minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds.toLong())         val seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds.toLong()) % 60         return String.format("%d:%02d", minutes, seconds)     }      // Clean up MediaPlayer and handler when activity is destroyed     override fun onDestroy() {         super.onDestroy()         handler.removeCallbacks(updateSeekBar)         if (::mediaPlayer.isInitialized) {             mediaPlayer.release()         }     } } 


Output:



Next Article
How to Build a Simple Music Player App Using Android Studio

A

ankur5oz5
Improve
Article Tags :
  • Android
  • Kotlin Android
  • Android Projects
  • Android apps

Similar Reads

  • 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 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
    6 min read
  • How to Build a Simple Torch App in Android using Kotlin?
    Torch Application is a very basic application that every beginner level android developer should definitely try to build while learning Android. In this article, we will be creating an application in which we will simply display a toggle button to switch on and switch off the torch. Note: If you are
    4 min read
  • How to Build a Simple Notes App in Android?
    Notes app is used for making short text notes, updating when you need them, and trashing when you are done. It can be used for various functions as you can add your to-do list in this app, some important notes for future reference, etc. The app is very useful in some cases like when you want quick a
    9 min read
  • How to Build a Simple Voice Typer App in Android using Java?
    Pre-requisites: Android App Development Fundamentals for BeginnersGuide to Install and Set up Android StudioHow to Create/Start a New Project in Android Studio?Running your first Android appSpinner in AndroidRecognizerIntent in Android In this article, we are going to build a simple Voice Typer app
    5 min read
  • How to Make a Scientific Calculator Android App using Kotlin?
    The calculator is the app that is present on every android device. This app comes pre-installed or we can also install another application from Play Store. It is one of the most used applications for college students for making any calculations. In this article, we will take a look at building a sim
    13 min read
  • How to Build a Simple Alarm Setter App in Android?
    In this article, we are going to see how to build a much interesting app named Alarm Setter. Alarm plays a vital role in our day-to-day life. Nowadays alarm has become our wake-up assistant. Every mobile phone is associated with an alarm app. We will create this app using android studio. Android Stu
    5 min read
  • How to Build a Simple e-Crackers App using Android?
    Pre-requisites: Android App Development Fundamentals for BeginnersGuide to Install and Set up Android StudioHow to Create/Start a New Project in Android Studio?Running your first Android appHow to add Lottie Animation in an Android AppMediaPlayer Class in Android In this article, we are going to bui
    12 min read
  • How to Build a Stock Market News Android App using Retrofit?
    Building a stock market news app can be a great way to stay informed about the latest developments in the financial world. This article will explore how to build a simple news app using Kotlin and Retrofit. A sample video is given below to get an idea about what we are going to do in this article. S
    12 min read
  • How to Build a Simple Note Android App using MVVM and Room Database?
    Android provides us a feature with which we can store users' data inside their mobile itself with different storage options such as Shared Preferences, SQLite database, and the Room Database. All the data storing techniques are having different use cases. In this article, we will specifically take a
    15+ min read
  • How to Build a Simple TikTok Clone Android App using Firebase?
    TikTok is a mobile application that can be downloaded on smartphones and tablets. It is available on both iOS and Android operating systems and can be downloaded for free from the respective app stores. TikTok is a social media platform where users can view short video content i.e. 15 to 60 secs. A
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences