Introduction
Developing a music player app provides hands-on experience with Android's MediaPlayer class, UI design, and handling external resources. This project will guide you through creating a functional music player that can play, pause, and stop audio playback.
Prerequisites
Before starting, ensure you have a basic understanding of:
- Android fundamentals
- Installing and setting up Android Studio
- Creating and running your first Android app
Step 1: Setting Up a New Android Project
- Open Android Studio and select "New Project".
- Choose "Empty Activity" and click "Next".
- Name your project (e.g., "MusicPlayerApp") and ensure the language is set to Java.
- Click "Finish" to create the project.
Step 2: Designing the User Interface
Designing an intuitive user interface is crucial for user engagement. For this app, we'll use the following components:
- ImageView: Displays the album art or a placeholder image.
- Buttons:
- Play Button: Starts the music.
- Pause Button: Pauses the music.
- Stop Button: Stops the music.
Step 3: Adding the Music File
To add a music file to your project:
- Create a
raw
directory:- Right-click on the
res
directory. - Select New > Android Resource Directory.
- Name it
raw
and set the resource type toraw
.
- Right-click on the
- Add your audio file:
- Place your audio file (e.g.,
sound.mp3
) into theraw
directory.
- Place your audio file (e.g.,
- Create a
-
DroidBlog – Native Blogger App Source Code (Android + Blogger API + Firebase)
₹6,999.00Original price was: ₹6,999.00.₹5,999.00Current price is: ₹5,999.00.
Step 4: Implementing the Functionality
Now, let's implement the functionality of our music player using the MediaPlayer class. This class provides an easy way to play audio files in Android apps.
1. Initialize MediaPlayer in Java
Create a MediaPlayer
object and assign it an audio file from the raw
folder.
package com.example.musicplayer;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// MediaPlayer instance
MediaPlayer music;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize MediaPlayer with a music file
music = MediaPlayer.create(this, R.raw.sound);
}
// Method to play music
public void playMusic(View v) {
if (!music.isPlaying()) {
music.start();
}
}
// Method to pause music
public void pauseMusic(View v) {
if (music.isPlaying()) {
music.pause();
}
}
// Method to stop music
public void stopMusic(View v) {
if (music.isPlaying()) {
music.stop();
// Reinitialize MediaPlayer after stopping
music = MediaPlayer.create(this, R.raw.sound);
}
}
}
2. Explanation of Code Functionality
- The
MediaPlayer.create()
method loads the audio file from theraw
folder. - The Play button starts playback.
- The Pause button pauses playback but allows resumption from the same point.
- The Stop button stops playback and resets the
MediaPlayer
instance.
Step 5: Running the App
Once the coding is complete, follow these steps to run your app:
Using an Emulator:
- Click Run in Android Studio.
- Select an Android Virtual Device (AVD).
- Wait for the app to launch.
Using a Physical Device:
- Enable Developer Mode on your phone.
- Turn on USB Debugging.
- Connect your phone via USB and select Run in Android Studio.
Note: Running the emulator requires at least 4GB RAM for smooth performance.
-
Speech AI Pro Text-to-Speech Android App
Rated 5.00 out of 5₹6,999.00Original price was: ₹6,999.00.₹5,999.00Current price is: ₹5,999.00.
Conclusion
Congratulations! 🎉 You've successfully created a Simple Music Player App in Android Studio. This project helped you understand:
✅ MediaPlayer class for playing audio.
✅ Android UI components like ImageView and Buttons.
✅ Handling media playback (play, pause, stop).
With these concepts, you can further improve the app by:
- Adding a seek bar for better control.
- Implementing a playlist feature.
- Using Kotlin instead of Java for modern development.
We are proud of our customers feedback!
I must say, it’s amazing! The code is well-documented, and I had no trouble customizing it for my needs. Totally worth the price!
I hired Droid Dev Solutions to develop my e-commerce website, and they exceeded my expectations!
As a beginner in Android development, I found the documentation and code structure very easy to understand. I deployed the app within a day