Android Simple Music Player

Posted Jun 28, 20204 min read

Function:play, pause, previous song, next song

Part of the demo:

MainActivity:

public class MainActivity extends Activity implements OnClickListener

{

//Get the song title and author text boxes in the interface

TextView title, author;

//play/pause, stop button

ImageButton play, stop;

//Previous song, next song

ImageButton pre, next;

ImageView cover;

ActivityReceiver activityReceiver;

public static final String CTL_ACTION =

"org.crazyit.action.CTL_ACTION";

public static final String UPDATE_ACTION =

"org.crazyit.action.UPDATE_ACTION";

//Define the music playing status, 1 means no playing; 2 means playing; 3 means pause

int status = 1;

String[]titleStrs = new String[]{"Wuji", "Lianhuawu", "Huangchengdu" };

String[]authorStrs = new String[]{" ", " ", " " };

Integer[]covers = new Integer[]{R.drawable.unrestrained, R.drawable.lotus,

R.drawable.barrencity};

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//Get the two buttons in the program interface

play =(ImageButton) this.findViewById(R.id.play);

stop =(ImageButton) this.findViewById(R.id.stop);

title =(TextView) findViewById(R.id.title);

author =(TextView) findViewById(R.id.author);

cover = findViewById(R.id.cover);

pre = this.findViewById(R.id.pre);

next = this.findViewById(R.id.next);

//Add listeners for the click events of the two buttons

play.setOnClickListener(this);

stop.setOnClickListener(this);

//Add listeners for the click events of the previous and next tracks

pre.setOnClickListener(this);

next.setOnClickListener(this);

activityReceiver = new ActivityReceiver();

//Create IntentFilter

IntentFilter filter = new IntentFilter();

//Specify the Action that BroadcastReceiver listens to

filter.addAction(UPDATE_ACTION);

//Register BroadcastReceiver

registerReceiver(activityReceiver, filter);

Intent intent = new Intent(this, MusicService.class);

//Start the background service

startService(intent);

}

//Custom BroadcastReceiver, responsible for listening to the broadcast from Service

public class ActivityReceiver extends BroadcastReceiver

{

@Override

public void onReceive(Context context, Intent intent)

{

//Get the update message in Intent, update represents the playback state

int update = intent.getIntExtra("update", -1);

//Get the current message in Intent, current represents the song currently playing

int current = intent.getIntExtra("current", -1);

if(current >= 0)

{

title.setText(titleStrs[current]);

author.setText(authorStrs[current]);

cover.setImageResource(covers[current]);

}

switch(update)

{

case 1:

play.setImageResource(R.drawable.play);

status = 1;

break;

//Control system enters playback state

case 2:

//Set to use the pause icon during playback

play.setImageResource(R.drawable.pause);

//Set the current state
Forex broker comparison http://www.fx61.com/brokerlist

status = 2;

break;

//The control system enters the suspended state

case 3:

//Set play icon in pause state

play.setImageResource(R.drawable.play);

//Set the current state

status = 3;

break;

}

}

}

@Override

public void onClick(View source)

{

//Create Intent

Intent intent = new Intent("org.crazyit.action.CTL_ACTION");

switch(source.getId())

{

//Press the play/pause button

case R.id.play:

intent.putExtra("control", 1);

break;

//press the stop button

case R.id.stop:

intent.putExtra("control", 2);

break;

case R.id.pre:

intent.putExtra("control",3);

case R.id.next:

intent.putExtra("control",4);

}

//Send broadcast, it will be received by BroadcastReceiver in Service component

sendBroadcast(intent);

}

}

MusicService:

public class MusicService extends Service

{

MyReceiver serviceReceiver;

AssetManager am;

String[]musics = new String[]{"unrestrained.mp3" +

"", "lotus.mp3",

"barrencity.mp3"};

MediaPlayer mPlayer;

//Current status, 1 means not playing; 2 means playing; 3 means pause

int status = 1;

//record the currently playing music

int current = 0;

@Override

public IBinder onBind(Intent intent)

{

return null;

}

@Override

public void onCreate()

{

super.onCreate();

am = getAssets();

//Create BroadcastReceiver

serviceReceiver = new MyReceiver();

//Create IntentFilter

IntentFilter filter = new IntentFilter();

filter.addAction(MainActivity.CTL_ACTION);

registerReceiver(serviceReceiver, filter);

//Create MediaPlayer

mPlayer = new MediaPlayer();

//Bind the listener for MediaPlayer playback completion event

mPlayer.setOnCompletionListener(new OnCompletionListener() //

{

@Override

public void onCompletion(MediaPlayer mp)

{

current++;

if(current >= 3)

{

current = 0;

}

//Send a broadcast notification Activity changes the text box

Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);

sendIntent.putExtra("current", current);

//Send broadcast, it will be received by BroadcastReceiver in Activity component

sendBroadcast(sendIntent);

//Prepare and play music

prepareAndPlay(musics[current]);

}

});

}

public class MyReceiver extends BroadcastReceiver

{

@Override

public void onReceive(final Context context, Intent intent)

{

int control = intent.getIntExtra("control", -1);

switch(control)

{

//play or pause

case 1:

//It was originally in a no-play state

if(status == 1)

{

//Prepare and play music

prepareAndPlay(musics[current]);

status = 2;

}

//It was originally playing

else if(status == 2)

{

//time out

mPlayer.pause();

//Change to the suspended state

status = 3;

}

//It was originally suspended

else if(status == 3)

{

//play

mPlayer.start();

//change state

status = 2;

}

break;

//stop sound

case 2:

//If it was playing or paused

if(status == 2 || status == 3)

{

//Stop play

mPlayer.stop();

status = 1;

}

case 3:

current--;

if(current <0)

current = 0;

prepareAndPlay(musics[current]);

case 4:

current++;

if(current> musics.length-1)

current = 0;

prepareAndPlay(musics[current]);

}

//Broadcast notification Activity to change icon, text box

Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);

sendIntent.putExtra("update", status);

sendIntent.putExtra("current", current);

//Send broadcast, it will be received by BroadcastReceiver in Activity component

sendBroadcast(sendIntent);

}

}

private void prepareAndPlay(String music)

{

try

{

//Open the specified music file

AssetFileDescriptor afd = am.openFd(music);

mPlayer.reset();

//Use MediaPlayer to load the specified sound file.

mPlayer.setDataSource(afd.getFileDescriptor(),

afd.getStartOffset(), afd.getLength());

//Prepare the sound

mPlayer.prepare();

//play

mPlayer.start();

}

catch(IOException e)

{

e.printStackTrace();

}

}

}