How to create Mp3 player in Android | Android Tutorial
How to create your own Mp3 Player in android | Android Tutorial
In this article, you will learn how you can create your own application. You will get in touch with many concepts like SeekBar, Runnable, and Handler. You'll learn how to use all these concepts in one application.
PREREQUISITE:
- Installation of Android Studio.
- Must be aware of Java language.
- Basic concepts of android.
What is MediaPlayer?
MediaPlayer is one of the applications of which every individual is aware of. When you travel, doing any work, or making yourself stress-free. This application comes in use to the very generation. Everyone loves music. So, why android lovers shouldn't create their MediaPlayer. Yes, Agreed that you have inbuilt MediaPlayer in smartphones but still it is a good chance to learn how it works internally.
With the help of the article, you can create your MediaPlayer.
PRINCIPAL ELEMENTS USED IN MAKING OF A MediaPlayer:
- LinearLayout: It is used as a Layout. It helps in aligning the elements properly. You can give the orientation either horizontally and vertically with the help of
android:orientation="vertical/horizontal"
- ImageView: ImageView element is used to add the image to the application. The images are stored in the res/drawable. It means it is kept in the drawable folder which is part of the resource folder. All the resources are stockpiled in the res folder only.
- Button: The Button is used to perform functions such as play, stop, pause, forward, backward the Music. The button is used to perform the following functions.
- SeekBar: SeekBar is referred to as an extension of the progress bar. It is used to fetch the timing of the music such as how the music is played. It extracts the current position of the music that helps in performing the forward and backward operations in Mediaplayer.
The main point to be noted that songs are added in the raw folder of the resource. Follow the directions to create the raw folder.
- Click Right on res.
- Point the cursor on New.
- Click on Android Resource Directory.
- Change value to the raw and raw folder created.
- Drag and drop the song into the raw folder.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="501dp"
android:src="@drawable/ghani" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/btnbackward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Backward"/>
<Button
android:id="@+id/btnplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="||"/>
<Button
android:id="@+id/btnforward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Forward"/>
</LinearLayout>
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Working of MediaPlayer in MainActivity.java file:
- Initially, create the object of elements such as Button, MediaPlayer, and Seekbar with the private access specifier. Allegedly private access specifier is used to taking the rights from the classes to use the element other than the class in which it is declared.
- Runnable and handler objects are also created. The runnable interface provides the common protocol to all the objects that which to access the code when it is active. The handler is used to handle the events that occur during the execution of the code.
- Assigning the referring ids of elements to created objects.
- Call the onClick function to implement the operations of play, stop, or pause.
package com.example.media_player;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import java.nio.channels.SeekableByteChannel;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnp,btnb,btnf;
private MediaPlayer player;
private SeekBar sk;
private Runnable runnable;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnp=findViewById(R.id.btnplay);
btnb=findViewById(R.id.btnbackward);
btnf=findViewById(R.id.btnforward);
sk=findViewById(R.id.seekbar);
handler= new Handler();
btnp.setOnClickListener(this);
btnb.setOnClickListener(this);
btnf.setOnClickListener(this);
player=MediaPlayer.create(this,R.raw.song);
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
sk.setMax(player.getDuration());
player.start();
changeSeekbar();
}
});
sk.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(b)
{
player.seekTo(i);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private void changeSeekbar() {
sk.setProgress(player.getCurrentPosition());
if(player.isPlaying())
{
runnable = new Runnable() {
@Override
public void run() {
changeSeekbar();
}
};
handler.postDelayed(runnable, 1000);
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnplay:
if(player.isPlaying())
{
player.pause();
btnp.setText(">");
}
else
{
player.start();
btnp.setText("||");
changeSeekbar();
}
break;
case R.id.btnforward:
player.seekTo(player.getCurrentPosition()+5000);
break;
case R.id.btnbackward:
player.seekTo(player.getCurrentPosition()-5000);
break;
}
}
}
OUTPUT: