Android mediaplayer simple and comprehensive application: lists all the upload files in sdcard, and you can click play!

Source: Internet
Author: User

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!

Related Article

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.