---
title: "Android MediaPlayer 简单综合应用------列出sdcard里所有.mp3文件,并且可以点击播放!"
author: "mmdev"
date: "2009-12-23T23:20:00Z"
tags: []
slug: "android-mediaplayer-sdcard-mp3"
draft: false
lastmod: "2009-12-23T23:20:00Z"
id: 990390
url: "https://mmdev.iteye.com/blog/990390"
---

大家好,我们今天要利用AndroidMediaPlayer

**Step 1**:preparation work.

**mksdcard 512M sdcard.img**

**create a new avd named AndroidSdcard**

![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091223/m2.jpg)

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:**

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;TextView android:id="@+id/text1" xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"  
android:layout\_width="wrap\_content"  
android:layout\_height="wrap\_content"/&gt;

**songlist.xml:**

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;LinearLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"  
android:orientation="vertical"  
android:layout\_width="fill\_parent"  
android:layout\_height="fill\_parent"&gt;

&lt;ListView android:id="@id/android:list"  
android:layout\_width="fill\_parent"  
android:layout\_height="fill\_parent"  
android:layout\_weight="1"  
android:drawSelectorOnTop="false"/&gt;

&lt;TextView android:id="@id/android:empty"  
android:layout\_width="fill\_parent"  
android:layout\_height="fill\_parent"  
android:text="No songs found on SD Card."/&gt;  
&lt;/LinearLayout&gt;

**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&lt;String&gt; songs = new ArrayList&lt;String&gt;();  
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 &gt; 0) {  
for (File file : home.listFiles( new Mp3Filter())) {  
songs.add(file.getName());  
}

ArrayAdapter&lt;String&gt; songList = new ArrayAdapter&lt;String&gt;(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:

![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091223/music1.jpg)

now u can enjoy the music.lol~

**if u wanna the source code please leave your email address, i will send u!**