A newbie in Android development comes across Media player usage in his/her project. So in this blog, I am here to tell you the basic of media player. One can use android inbuilt media player or can customize media player according to the need.
Here i am using inbuilt Media player to start with,
In your XML,copy the following code:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Music Player"
android:textSize="35dp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:src="@drawable/icon_all_post" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text=">>"
android:visibility="invisible" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/imageView"
android:text="Pause" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_toEndOf="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="play" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_alignEnd="@+id/textview"
android:layout_alignLeft="@+id/textview"
android:layout_alignRight="@+id/textview"
android:layout_alignStart="@+id/textview" />
<TextView
android:id="@+id/totalTimeOfSongText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/seekBar"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/timeUpdateText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/seekBar"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/songNameText"
android:gravity="end"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/songNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/totalTimeOfSongText"
android:layout_alignBottom="@+id/totalTimeOfSongText"
android:layout_centerHorizontal="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
In this XML, I am having play, pause button,Seekbar,and three text view.
Before going to java file,you must add folder name raw inside res directory of your project.After adding raw folder,copy +paste a song in raw folder created.Rebuild your project and you can see raw folder inside res directory in your project.
Now java file,MainActivity.java
private static final String KEY_SEEK_PROGRESS = "key_progress";
private MediaPlayer mediaPlayer;
private Button pause, play;
private TextView totalTimeOfSongText, timeUpdateText, songNameText;
private SeekBar seekbar;
private double startTime = 0;
private Handler myHandler = new Handler();
private int seekProgress = 0;
In onCreate method of your activity,initialize all the buttons and text view
pause = (Button) findViewById(R.id.button2);
play = (Button) findViewById(R.id.button3);
totalTimeOfSongText = (TextView) findViewById(R.id.totalTimeOfSongText);
timeUpdateText = (TextView) findViewById(R.id.timeUpdateText);
songNameText = (TextView) findViewById(R.id.songNameText);
Add the following code in onResume of your activity,
myHandler.post(UpdateSongTime);
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
play.setEnabled(false);
pause.setEnabled(true);
mediaPlayer.start();
} else {
pause.setEnabled(false);
mediaPlayer = MediaPlayer.create(this, R.raw.song);
}
songNameText.setText("Song.mp3");
seekbar = (SeekBar) findViewById(R.id.seekBar);
seekbar.setClickable(false);
seekbar.setMax(mediaPlayer.getDuration());
totalTimeOfSongText.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) mediaPlayer.getDuration()),
TimeUnit.MILLISECONDS.toSeconds((long) mediaPlayer.getDuration()) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mediaPlayer.getDuration()))));
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()) {
Toast.makeText(MainActivity.this, "Playing", Toast.LENGTH_SHORT).show();
if (seekProgress != 0) {
mediaPlayer.seekTo(seekProgress);
mediaPlayer.start();
} else {
mediaPlayer.start();
}
} else {
Toast.makeText(MainActivity.this, "already playing", Toast.LENGTH_SHORT).show();
}
pause.setEnabled(true);
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "stopped at" + mediaPlayer.getCurrentPosition(), Toast.LENGTH_SHORT).show();
mediaPlayer.pause();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
timeUpdateText.setText("0 min,0sec");
mp.start();
}
});
private Runnable UpdateSongTime = new Runnable() {
@SuppressLint("DefaultLocale")
public void run() {
startTime = mediaPlayer.getCurrentPosition();
timeUpdateText.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
seekbar.setProgress((int) startTime);
myHandler.postDelayed(this, 100);
}
};
That's all.Media player is ready and you can run your project and enjoy music :)
0 Comment(s)