Hello! Today we will learn how to create a simple app that plays MP3 files on Android using Kotlin. In this tutorial, you will learn how to utilize various features of the system based on the basic concepts of Kotlin. This app will provide a basic user interface (UI) and have functionalities to load, play, pause, and stop MP3 files.
1. Project Setup
Open Android Studio and create a new project. Follow the steps below:
- Click “New Project” in Android Studio.
- Select “Empty Activity” and then click “Next”.
- Enter the project name and set the package name and the location to save.
- Select “Kotlin” as the language and click “Finish” to create the project.
2. Add Dependencies
We will use Android’s MediaPlayer class to play the MP3 files. Although no dependency library is required, you may add the following library to make the UI more appealing.
implementation "com.google.android.material:material:1.3.0"
3. Designing the User Interface (UI)
Now let’s set up the user interface. Open the activity_main.xml
file and add the following code:
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<Button
android:id="@+id/btnPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:layout_below="@id/btnPlay"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:layout_below="@id/btnPause"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
</RelativeLayout>
4. Configure MainActivity
Now open the MainActivity.kt
file and add the logic to play the MP3 file. First, declare the MediaPlayer object and write code to handle the button click events:
import android.media.MediaPlayer
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var mediaPlayer: MediaPlayer
private lateinit var btnPlay: Button
private lateinit var btnPause: Button
private lateinit var btnStop: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnPlay = findViewById(R.id.btnPlay)
btnPause = findViewById(R.id.btnPause)
btnStop = findViewById(R.id.btnStop)
mediaPlayer = MediaPlayer.create(this, R.raw.sample) // You need to add the sample.mp3 file to the res/raw folder.
btnPlay.setOnClickListener {
if (!mediaPlayer.isPlaying) {
mediaPlayer.start()
}
}
btnPause.setOnClickListener {
if (mediaPlayer.isPlaying) {
mediaPlayer.pause()
}
}
btnStop.setOnClickListener {
if (mediaPlayer.isPlaying) {
mediaPlayer.stop()
mediaPlayer.prepare() // Prepare the media for playback.
}
}
}
override fun onDestroy() {
super.onDestroy()
mediaPlayer.release() // Release resources when the activity is finished
}
}
5. Adding MP3 Files
The MP3 file used in the above code should be located in the res/raw
folder. Create a raw
folder within the res
folder of the project, and add the MP3 file you want to play (e.g., sample.mp3
). This will allow the MediaPlayer to play the file.
6. Running the App
Now that everything is set up, let’s run the app. Click the run button at the top of Android Studio to launch the app in an emulator or on a real device.
7. Enhancing User Experience
The basic MP3 playback functionality is complete. However, you can implement additional features to enhance the app’s usability. For example:
- Add a SeekBar to display the playback position
- Add a playlist feature
- Display a notification when the playback is complete
- Change button states on pause/resume
These features can be easily implemented using various components of Android. If you want to take it a step further, consider looking into Jetpack libraries and the MVVM architecture to improve your code structure.
8. Conclusion
In this tutorial, we created a simple app with MP3 playback capabilities using Kotlin on Android. I hope this process helps you take your first step into the exciting world of mobile app development. Additionally, feel free to continue challenging yourself by adding various features or integrating with Bluetooth devices to play music!
Thank you!