Import Java.io.File;
Import java.util.ArrayList;
Import java.util.List;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.DialogInterface;
Import Android.media.MediaPlayer;
Import Android.os.Bundle;
Import android.view.KeyEvent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.ArrayAdapter;
Import Android.widget.Button;
Import Android.widget.ListView;
public class Mainactivity extends Activity {
Private MediaPlayer MP;
Private list<string> List = new arraylist<string> ();
private int location = 0; //Index of the currently playing song
Private Button play;
Private Button up;
Private Button Next;
Private Button Replay;
Private Boolean flag = false;
Private Button stop;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Play = (Button) Findviewbyid (R.id.bt_play);
up = (Button) Findviewbyid (R.ID.BT_UP);
Next = (Button) Findviewbyid (R.id.bt_next);
Replay = (Button) Findviewbyid (R.id.bt_replay);
Stop = (Button) Findviewbyid (r.id.bt_stop);
Replay.setenabled (FALSE);
MP = new MediaPlayer ();
Showlist ();
Play.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
if (flag = = False) {
Playmusic (List.get (location));
Play.settext ("| |");
Flag = true;
Replay.setenabled (TRUE);
} else {
Yesorno ();
}
}
});
Up.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Upmusic ();
}
});
Next.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Nextmusic ();
}
});
Replay.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Mp.seekto (0);
Mp.start ();
Play.settext ("| |");
}
});
Stop.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Mp.stop ();
Flag = false;
Play.settext ("");
Replay.setenabled (FALSE);
}
});
}
/*
*
* Show music files in the ListView
*/
private void Showlist () {
GetFile ("/mnt/sdcard/qqmusic/import/"); //Get all the music files on the SD card
arrayadapter<string> adapter = new Arrayadapter<string> (This,
Android. R.layout.simple_list_item_1, list); //Create an adaptation Device
ListView lv_list = (ListView) Findviewbyid (R.id.lv_list);
Lv_list.setadapter (adapter);
Lv_list.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview<?> arg0, View arg1, int arg2,
Long Arg3) {
location = arg2;
Playmusic (List.get (location));
}
});
}
/*
* Get music files
*/
public void getFile (String URL) {
File files = new file (URL);
file[] File = Files.listfiles ();
for (File f:file) {
if (F.isdirectory ()) { //If it is a folder-(there is a problem here, address write full code but this can work, otherwise null, need to improve )
GetFile (F.getabsolutepath ()); //Recursive call--(there is a problem here, address write full code but this can work, otherwise null, need to improve )
} else {
if (Isaudiofile (F.getpath ())) { //if it is a prescribed file format
List.add (F.getpath ()); //Add a file to the collection
}
}
}
}
/*
*
* Determine file format
*/
Private Boolean Isaudiofile (String path) {
if (Path.contains ("MP3")) {
return true;
}
return false;
}
/*
*
* Play
*/
public void Playmusic (String path) {
if (mp.isplaying ()) {
Mp.stop ();
}
Mp.reset (); //Reset MediaPlayer
try {
Mp.setdatasource (path);
Mp.prepare ();
Mp.start ();
Play.settext ("| |");
Flag = true;
Replay.setenabled (TRUE);
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
/*
*
* Pause, continue
*/
public void Yesorno () {
if (MP! = null && mp.isplaying ()) {
Mp.pause ();
Play.settext ("");
} else {
Mp.start ();
Play.settext ("| |");
}
}
/*
*
* Next Song
*/
public void Nextmusic () {
if (++location > List.size ()) {
Location = 0;
}
Play.settext ("| |");
Flag = true;
Replay.setenabled (TRUE);
Playmusic (List.get (location));
}
/*
*
* Last Song
*/
public void Upmusic () {
if (--location < 0) {
Location = List.size ()-1;
}
Play.settext ("| |");
Flag = true;
Replay.setenabled (TRUE);
Playmusic (List.get (location));
}
@Override
protected void OnDestroy () {
if (mp.isplaying ()) {
Mp.stop ();
}
Mp.release ();
Super.ondestroy ();
}
This article is from the "Android Small Notes" blog, please be sure to keep this source http://dreamwing.blog.51cto.com/9872128/1619519
Music Playback Function Button implementation