How To Make Simple Music Player In Android

Hello guys hope you enjoy this all type example... In this Example we will learn how to make a simple music player with notification , play , pause , next , previous .

1. AudioPlayerActivity

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class AudioPlayerActivity extends Activity {

Button btnBack,btnNext;
static Button btnPause;
static Button btnPlay;
static TextView textNowPlaying,textAlbumArtist,textComposer;
static LinearLayout linearLayoutPlayer;
ProgressBar progressBar;
static Context context;
TextView textBufferDuration, textDuration;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_player);
context = this;
init();
}

private void init() {
getViews();
setListeners();
progressBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.white), Mode.SRC_IN);
PlayerConstants.PROGRESSBAR_HANDLER = new Handler(){
@Override
        public void handleMessage(Message msg){
Integer i[] = (Integer[])msg.obj;
textBufferDuration.setText(UtilFunctions.getDuration(i[0]));
textDuration.setText(UtilFunctions.getDuration(i[1]));
progressBar.setProgress(i[2]);
    }
};
}

private void setListeners() {
btnBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Controls.previousControl(getApplicationContext());
}
});

btnPause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Controls.pauseControl(getApplicationContext());
}
});

btnPlay.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Controls.playControl(getApplicationContext());
}
});

btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Controls.nextControl(getApplicationContext());
}
});
}

public static void changeUI(){
updateUI();
changeButton();
}

private void getViews() {
btnBack = (Button) findViewById(R.id.btnBack);
btnPause = (Button) findViewById(R.id.btnPause);
btnNext = (Button) findViewById(R.id.btnNext);
btnPlay = (Button) findViewById(R.id.btnPlay);
textNowPlaying = (TextView) findViewById(R.id.textNowPlaying);
linearLayoutPlayer = (LinearLayout) findViewById(R.id.linearLayoutPlayer);
textAlbumArtist = (TextView) findViewById(R.id.textAlbumArtist);
textComposer = (TextView) findViewById(R.id.textComposer);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
textBufferDuration = (TextView) findViewById(R.id.textBufferDuration);
textDuration = (TextView) findViewById(R.id.textDuration);
textNowPlaying.setSelected(true);
textAlbumArtist.setSelected(true);
}

@Override
protected void onResume() {
super.onResume();
boolean isServiceRunning = UtilFunctions.isServiceRunning(SongService.class.getName(), getApplicationContext());
if (isServiceRunning) {
updateUI();
}
changeButton();
}

public static void changeButton() {
if(PlayerConstants.SONG_PAUSED){
btnPause.setVisibility(View.GONE);
btnPlay.setVisibility(View.VISIBLE);
}else{
btnPause.setVisibility(View.VISIBLE);
btnPlay.setVisibility(View.GONE);
}
}

private static void updateUI() {
try{
String songName = PlayerConstants.SONGS_LIST.get(PlayerConstants.SONG_NUMBER).getTitle();
String artist = PlayerConstants.SONGS_LIST.get(PlayerConstants.SONG_NUMBER).getArtist();
String album = PlayerConstants.SONGS_LIST.get(PlayerConstants.SONG_NUMBER).getAlbum();
String composer = PlayerConstants.SONGS_LIST.get(PlayerConstants.SONG_NUMBER).getComposer();
textNowPlaying.setText(songName);
textAlbumArtist.setText(artist + " - " + album);
if(composer != null && composer.length() > 0){
textComposer.setVisibility(View.VISIBLE);
textComposer.setText(composer);
}else{
textComposer.setVisibility(View.GONE);
}
}catch(Exception e){
e.printStackTrace();
}
try{
long albumId = PlayerConstants.SONGS_LIST.get(PlayerConstants.SONG_NUMBER).getAlbumId();
Bitmap albumArt = UtilFunctions.getAlbumart(context, albumId);
if(albumArt != null){
linearLayoutPlayer.setBackgroundDrawable(new BitmapDrawable(albumArt));
}else{
linearLayoutPlayer.setBackgroundDrawable(new BitmapDrawable(UtilFunctions.getDefaultAlbumArt(context)));
}
}catch(Exception e){
e.printStackTrace();
}
}
}

2. audio_player

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayoutPlayer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/ssss"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#70000000"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textNowPlaying"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:gravity="center_horizontal"
            android:marqueeRepeatLimit="marquee_forever"
            android:paddingLeft="5dp"
            android:paddingTop="20dp"
            android:singleLine="true"
            android:text="@string/idle"
            android:textColor="@color/white"
            android:textSize="30dp" />

        <TextView
            android:id="@+id/textAlbumArtist"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:gravity="center_horizontal"
            android:marqueeRepeatLimit="marquee_forever"
            android:paddingLeft="5dp"
            android:singleLine="true"
            android:textColor="@color/white"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/textComposer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:paddingLeft="5dp"
            android:textColor="@color/white"
            android:textSize="15dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/textBufferDuration"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textColor="@color/white" />

            <TextView
                android:id="@+id/textDuration"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:textColor="@color/white" />
        </LinearLayout>

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center|bottom"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="#521050"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnBack"
                android:layout_width="@dimen/button_width"
                android:layout_height="@dimen/button_width"
                android:layout_margin="10dp"
                android:background="@mipmap/ic_action_previous" />

            <View
                android:layout_width="0.5dp"
                android:layout_height="@dimen/button_width"
                android:background="@android:color/darker_gray" />

            <Button
                android:id="@+id/btnPause"
                android:layout_width="@dimen/button_width"
                android:layout_height="@dimen/button_width"
                android:layout_margin="10dp"
                android:background="@mipmap/ic_action_pause"
                android:visibility="gone" />

            <Button
                android:id="@+id/btnPlay"
                android:layout_width="@dimen/button_width"
                android:layout_height="@dimen/button_width"
                android:layout_margin="10dp"
                android:background="@mipmap/ic_action_play" />

            <View
                android:layout_width="0.5dp"
                android:layout_height="@dimen/button_width"
                android:background="@android:color/darker_gray" />

            <Button
                android:id="@+id/btnNext"
                android:layout_width="@dimen/button_width"
                android:layout_height="@dimen/button_width"
                android:layout_margin="10dp"
                android:background="@mipmap/ic_action_next" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>