1. Search for controls
2. Scan the map3 file under SDcard
3. Add the scanned files to the collection list and show them
Setadapter ();
4. Set entries for the ListView Click Listen Events (turn on music)
Lv.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview<?> parent, view view,
int position, long ID) {
try {
Cu = position;
Turn off the timer task (get the progress of the song in real time)
Stoptimer ();
Call the way to turn on music
Startmusic (position);
} catch (IllegalArgumentException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SecurityException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IllegalStateException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
});
5. How to start music
private void Startmusic (int position) throws IOException {
Close a song You're playing (avoid playing multiple songs)
if (player! = null && player.isplaying ()) {
Player.stop ();
Player.release ();
Player = null;
}
Play the selected song
Player = new MediaPlayer ();
Player.setdatasource (List.get (position). GetPath ());
Player.prepare ();
Player.start ();
Get the total length of the current song
int duration = Player.getduration ();
Assign the total length of the song to Seekbar
Seekbar.setmax (duration);
Enable Seekbar to get the progress of a song in real time
Getprogress ();
}
6. Get the progress of the song in real time
private void Getprogress () {
Timer = new timer ();
Timer.schedule (New TimerTask () {
@Override
public void Run () {
Get the progress of a song
int p = player.getcurrentposition ();
Assign the progress of the Get song to Seekbar
Seekbar.setprogress (P);
}
}, 0, 2000);
}
7, implement drag-and-drop bar, songs can be dragged to the point and then play the song (Seekbar listening events)
Monitor the progress of Seekbar
Seekbar.setonseekbarchangelistener (New Onseekbarchangelistener () {
@Override
public void Onstoptrackingtouch (SeekBar SeekBar) {
Get Progress on progress bar
int p = seekbar.getprogress ();
Assign the progress bar progress to the song
Player.seekto (P);
Start music continue to get the progress of the song
Getprogress ();
}
@Override
public void Onstarttrackingtouch (SeekBar SeekBar) {
Cancel Timer Task
Stoptimer ();
}
@Override
public void onprogresschanged (SeekBar SeekBar, int progress,
Boolean Fromuser) {
}
});
}
8. Jump from this activity to another activity, and then return to the song to continue playing
@Override
protected void OnStop () {
TODO auto-generated Method Stub
Super.onstop ();
System.out.println ("main----onStop");
Stoptimer ();
Record the progress of the current song
CurrentPosition = Player.getcurrentposition ();
Sharedpreferences share = getsharedpreferences ("current", 0);
Share.edit (). Putint ("id", currentposition). Putint ("NameID", Cu)
. commit ();
Pause Song
Player.stop ();
}
@Override
protected void Onrestart () {
TODO auto-generated Method Stub
Super.onrestart ();
System.out.println ("main----onrestart");
try {
Start the song again
Sharedpreferences share = getsharedpreferences ("current", 0);
int i = share.getint ("id", 0);
Player.prepare ();
Player.seekto (i);
Player.start ();
Seekbar get the song progress in real Time
Getprogress ();
} catch (IllegalStateException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IllegalArgumentException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SecurityException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}/*
* catch (IOException e) {//TODO auto-generated catch block
* E.printstacktrace (); }
*/catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
9.//Close Timer
public void Stoptimer ()
{
if (timer!=null)
{
Timer.cancel ();
}
}
MediaPlayer Play Music Control