Read the MP3 file under the SD Card folder and play the MP3 file

Source: Internet
Author: User

First get all the MP3 files under the path path of the SD card and save the file name and size to the list array (this code is defined in the Fileutils Class):

/**
* Read the name and size of the Mp3 file in the directory
*/
Public list<mp3info> getmp3files (String path) {
Sdcardroot = Environment.getexternalstoragedirectory ()
. GetAbsolutePath (); Get the path name of the SD card
list<mp3info> Mp3infos = new arraylist<mp3info> ();
File File = new file (sdcardroot + file.separator + path);
file[] files = file.listfiles ();
for (int i = 0; i < files.length; i++) {
if (Files[i].getname (). EndsWith (". mp3")) {
Mp3info mp3info = new Mp3info ();
Mp3info.setmp3name (Files[i].getname ());
Mp3info.setmp3size (Files[i].length () + "");
Mp3infos.add (Mp3info); This Mp3info object contains MP3 file name and file size
}
}
return Mp3infos;
}

The current activity inherits from Listactivity, which calls the Getmp3files () method in this activity's Onresume method to get the Mp3 file under the path

@Override
protected void Onresume () {
FileUtils FileUtils = new FileUtils ();
Mp3infos = Fileutils.getmp3files ("mp3/");
listfor (Iterator Iterator = Mp3infos.iterator (); Iterator.hasnext ();) {
Mp3info Mp3info = (mp3info) iterator.next ();
hashmap<string,string> map = new hashmap<string,string> ();
Map.put ("Mp3_name", Mp3info.getmp3name ());
Map.put ("size_id", Mp3info.getmp3size ());
List.add (map);
}
Simpleadapter simpleadapter = new Simpleadapter (this,list,r.layout.mp3info_item,new string[]{"Mp3_name", "size_id"},    New int[] {r.id.mp3_name,r.id.size_id}); Only two textview,id in the Mp3info_item.xml file are Mp3_name and size_id
Setlistadapter (Simpleadapter);
Super.onresume ();
}

When you click a row in the current activity, the Mp3 file for the current row is played:

@Override
protected void Onlistitemclick (ListView l, View v, int position, long ID) {
if (Mp3infos! = null) {
Mp3info mp3info = mp3infos.get (position);
Intent Intent = new Intent ();
Intent.putextra ("Mp3info", mp3info);
Intent.setclass (This,playeractivity.class);
StartActivity (Intent);
}
Super.onlistitemclick (l, V, position, id);
}

The Playeractivity.class class uses the service to play the selected Mp3 file, as defined below:

public class Playeractivity extends Activity {

Private ImageButton Beginbutton = null;
Private ImageButton Pausebutton = null;
Private ImageButton Stopbutton = null;
Private Mp3info mp3info = null;
@Override
protected void onCreate (Bundle savedinstancestate) {
Setcontentview (r.layout . player);
Super.oncreate (savedinstancestate);
Intent Intent = Getintent ();
Mp3info = (mp3info) Intent.getserializableextra ("Mp3info");
Beginbutton = (ImageButton) Findviewbyid (R.id.begin);
Pausebutton = (ImageButton) Findviewbyid (r.id.pause);
Stopbutton = (ImageButton) Findviewbyid (r.id.stop);
Buttonclicklistener listener = new Buttonclicklistener ();
Beginbutton.setonclicklistener (listener);
Pausebutton.setonclicklistener (listener);
Stopbutton.setonclicklistener (listener);
}

Class Buttonclicklistener implements Onclicklistener {

@Override
public void OnClick (View v) {
if (v.getid () = = R.id.begin) {//Play button
Creates a intent object that notifies the service to start playing MP3
Intent Intent = new Intent ();
Intent.setclass (Playeractivity.this, Playerservice.class);
Intent.putextra ("Mp3info", mp3info);
Intent.putextra ("MSG", AppConstant.PlayerMsg.PLAY_MSG);
Start Service
StartService (Intent);
} else if (v.getid () = = R.id.pause) {
Notifies the service to pause playback MP3
Intent Intent = new Intent ();
Intent.setclass (Playeractivity.this, Playerservice.class);
Intent.putextra ("MSG", AppConstant.PlayerMsg.PAUSE_MSG);
StartService (Intent);
} else if (v.getid () = = R.id.stop) {
Notifies the service to stop MP3 files
Intent Intent = new Intent ();
Intent.setclass (Playeractivity.this, Playerservice.class);
Intent.putextra ("MSG", AppConstant.PlayerMsg.STOP_MSG);
StartService (Intent);
}
}

}
}

Playerservice is defined as follows:

public class Playerservice extends Service {

Private Boolean isplaying = false;
Private Boolean ispause = false;
Private Boolean isreleased = false;
Private MediaPlayer MediaPlayer = null;
Private Mp3info mp3info = null;
Private arraylist<queue> queues = null;

@Override
Public IBinder Onbind (Intent arg0) {
TODO auto-generated Method Stub
return null;
}

This event is triggered every time a intent object is sent from activity to service
@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
Mp3info = (mp3info) Intent.getserializableextra ("Mp3info");
int MSG = Intent.getintextra ("MSG", 0);
if (mp3info! = null) {
if (MSG = = AppConstant.PlayerMsg.PLAY_MSG) {
Play (Mp3info);
}
}
else{
if (MSG = = AppConstant.PlayerMsg.PAUSE_MSG) {
Pause ();
}
else if (MSG = = AppConstant.PlayerMsg.STOP_MSG) {
Stop ();
}
}
Return Super.onstartcommand (Intent, flags, Startid);
}
private void Play (Mp3info mp3info) {
if (!isplaying) {
String path = Getmp3path (Mp3info);
MediaPlayer = Mediaplayer.create (This, Uri.parse ("file://" + path);
Mediaplayer.setlooping (TRUE); Set the song Loop to play
Mediaplayer.start ();
IsPlaying = true;
isreleased = false;
}
}

private void Pause () {
if (MediaPlayer! = null) {
if (isplaying) {
Mediaplayer.pause ();
} else{
Mediaplayer.start ();
}
isplaying = isplaying? False:true;
}
}
private void Stop () {
if (mediaplayer!=null) {
if (isplaying) {
if (!isreleased) {
Mediaplayer.stop ();
Mediaplayer.release ();
Isreleased = true;
IsPlaying = false;
}
}
}
}
Private String Getmp3path (Mp3info mp3info) {
String sdcardroot = Environment.getexternalstoragedirectory (). GetAbsolutePath ();
String Path = sdcardroot + File.separator + "MP3" + File.separator + mp3info.getmp3name ();
return path;
}

Read the MP3 file under the SD Card folder and play the MP3 file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.