The Android platform provides an excellent environment for developing various apps. In this course, we will learn how to create a basic MP3 playback app using Java. This course is targeted at developers who already have a basic understanding of Android development, and you will learn app development from the basics to advanced features as you progress through the project.
Basic Concepts
An MP3 playback app is an app that provides functionalities such as playing, pausing, stopping music files, and changing tracks. To create such an app, we will utilize Android’s Media Player API, which is necessary for handling audio files. The app will also provide a User Interface (UI) that allows users to operate easily.
Setting Up the Development Environment
To develop Android apps, you must first set up the development environment. Please follow the steps below:
- Install Android Studio: Android Studio is the official IDE for Android development. Download and install the latest version.
- Create a New Project: After launching Android Studio, select “New Project.” Choose “Empty Activity,” enter a project name, and click “Finish.”
- Check Gradle Settings: Once the project is created, check the Gradle settings to ensure that the necessary libraries are included.
App UI Design
We will create an XML layout file to design the app’s user interface. Open the “res/layout” folder, and create “activity_main.xml” file, then enter the following code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttonPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play" /
>
<Button
android:id="@+id/buttonPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:layout_below="@id/buttonPlay" /
>
<Button
android:id="@+id/buttonStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:layout_below="@id/buttonPause" /
>
<TextView
android:id="@+id/textViewStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Status: Stopped"
android:layout_below="@id/buttonStop" /
>
</RelativeLayout>
The above code sets up a basic user interface, which includes three buttons and a TextView to display the status.
Writing the Java Code
Now we will write the Java code to implement the actual MP3 playback functionality. Open the “MainActivity.java” file and add the following code:
package com.example.mp3player;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private Button buttonPlay, buttonPause, buttonStop;
private TextView textViewStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonPlay = findViewById(R.id.buttonPlay);
buttonPause = findViewById(R.id.buttonPause);
buttonStop = findViewById(R.id.buttonStop);
textViewStatus = findViewById(R.id.textViewStatus);
mediaPlayer = MediaPlayer.create(this, R.raw.sample_mp3); // sample_mp3 should be in the res/raw folder.
buttonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.start();
textViewStatus.setText("Status: Playing");
}
});
buttonPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
textViewStatus.setText("Status: Paused");
}
}
});
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying() || mediaPlayer.isLooping()) {
mediaPlayer.stop();
mediaPlayer.prepareAsync(); // For preparing it again
textViewStatus.setText("Status: Stopped");
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}
The above code implements the functionality to play, pause, and stop the MP3 file using MediaPlayer. It updates the status and reflects it in the UI every time the user clicks a button.
Adding Audio Files
To add the MP3 files for use in the app, follow the steps below:
- Create a raw folder in the res directory: Right-click the “res” folder and select “New” → “Android Resource Directory.” Choose “raw” as the Resource type, and click “OK.”
- Add MP3 files: Copy and paste the MP3 file you want to use (e.g., sample_mp3.mp3) into the created “raw” folder.
Running the App
Now that all the settings are complete, click the “Run” button in Android Studio to run the app. The app will run on the emulator or a real device, and buttons to play the MP3 file will be displayed.
Conclusion
In this course, we learned how to create a basic Android MP3 playback app using Java. Besides creating a simple media player, you may also consider implementing additional features (like playlists, volume control, user settings, etc.) to develop a more advanced app.
By completing this simple project, you have laid the foundation for Android app development. We hope you continue to develop various apps and deepen your understanding of the Android platform.
Additional Resources
For further study on Android development, please refer to the following resources: