Hello everyone, we are going to use Android mediaplayer today
Step 1: Preparation work.
Mksdcard 512 M sdcard. img
Create a new AVD named androidsdcard
Push songs into sdcard (before you push, you make sure your AVD is running, else the operation of push will not work ):
ADB push F:/music/1.mp3/sdcard
Step 2: Layout UI desigen:
Create two. xml files we called song_item.xml and songlist. xml the Code are:
Song_item.xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<Textview Android: Id = "@ + ID/text1" xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
Songlist. xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Listview Android: Id = "@ ID/Android: List"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_weight = "1"
Android: drawselectid Top = "false"/>
<Textview Android: Id = "@ ID/Android: empty"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "no songs found on SD card."/>
</Linearlayout>
Step 3: The Core code meusicdemo. Java:
Package com. Android. test;
Import java. Io. file;
Import java. Io. filenamefilter;
Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. List;
Import Android. App. listactivity;
Import Android. Media. mediaplayer;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. view;
Import Android. widget. arrayadapter;
Import Android. widget. listview;
Class mp3filter implements filenamefilter {
Public Boolean accept (File Dir, string name ){
Return (name. endswith (". MP3 "));
}
}
Public class meusicdemo extends listactivity {
Private Static final string media_path = new string ("/sdcard /");
Private list <string> songs = new arraylist <string> ();
Private mediaplayer MP = new mediaplayer ();
@ Override
Public void oncreate (bundle icicle ){
Try {
Super. oncreate (icicle );
Setcontentview (R. layout. songlist );
Updatesonglist ();
} Catch (nullpointerexception e ){
Log. V (getstring (R. String. app_name), E. getmessage ());
}
}
Public void updatesonglist (){
File home = new file (media_path );
If (home. listfiles (New mp3filter (). length> 0 ){
For (File file: Home. listfiles (New mp3filter ())){
Songs. Add (file. getname ());
}
Arrayadapter <string> songlist = new arrayadapter <string> (this, R. layout. song_item, songs );
Setlistadapter (songlist );
}
}
@ Override
Protected void onlistitemclick (listview L, view V, int position, long ID ){
Try {
MP. Reset ();
MP. setdatasource (media_path + songs. Get (position ));
MP. Prepare ();
MP. Start ();
} Catch (ioexception e ){
Log. V (getstring (R. String. app_name), E. getmessage ());
}
}
}
Step 4: Run it. The result like this:
Now u can enjoy the music. Lol ~
If u wanna the source code please leave your email address, I will send u!