<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/AbsoluteLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false" />
<AbsoluteLayout
android:id="@+id/AbsoluteLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10px"
android:background="@drawable/last"
android:text="" >
</Button>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="70px"
android:background="@drawable/stop"
android:text="" >
</Button>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="130px"
android:background="@drawable/start"
android:text="" >
</Button>
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="190px"
android:background="@drawable/pause"
android:text="" >
</Button>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="250px"
android:background="@drawable/next"
android:text="" >
</Button>
</AbsoluteLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01" android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"></ImageView>
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:text="@+id/TextView01"
android:id="@+id/song_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize ="15px">
</TextView>
<TextView android:text="@+id/info" android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10px">
</TextView>
</LinearLayout>
</LinearLayout>
package com.ouling.ex_musicPlayer;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import android.app.Activity;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;
public class ex_musicPlayer extends ListActivity {
// 播放对象
private MediaPlayer m_musicplayer;
// 播放列表
private List<String> m_playlist = new ArrayList<String>();
// 当前播放索引位置
private int m_list_item = 0;
// 音乐路径
private static String m_musicpath = new String("/sdcard/");
public Button last;
public Button stop;
public Button start;
public Button pause;
public Button next;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_musicplayer = new MediaPlayer();
findView();// 获取按钮
musicList();// 获取播放列表
listener();// 监听
}
private void listener() {
// TODO Auto-generated method stub
// 停止
this.stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (m_musicplayer!=null) {
m_musicplayer.stop();
}
}
});
// 开始
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
playMusic(m_playlist.get(m_list_item));
}
});
// 下一首
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
nextMusic();
}
});
// 暂停
pause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (m_musicplayer.isPlaying()) {
m_musicplayer.pause();
} else {
m_musicplayer.start();
}
}
});
// 上一首
last.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
lastMusic();
}
});
}
// 获取SD音乐
private void musicList() {
// TODO Auto-generated method stub
m_playlist.clear();
search(m_musicpath, "mp3", m_playlist);
SimpleAdapter adapter = new SimpleAdapter(this, this.getData(),
R.layout.m_musicitem,
new String[] { "song_name", "info", "img" }, new int[] {
R.id.song_name, R.id.info, R.id.img });
setListAdapter(adapter);
}
// 遍历路径下指定后缀名
private void search(String dir, final String suffix, List<String> list) {
File file = new File(dir);
// 遍历该目录中所有文件
File[] files = file.listFiles();
if ((files != null) && (files.length > 0)) {
for (File tmpfile : files) {
// 如果是文件夹,继续遍历该目录
if (tmpfile.isDirectory()) {
search(tmpfile.getPath(), suffix, list);
} else {
//判断文件后缀名
if (tmpfile.getPath().endsWith(suffix)) {
list.add(tmpfile.getPath());
}
}
}
}
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> m_list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
for (int i = 0; i < m_playlist.size(); i++) {
map = new HashMap<String, Object>();
//获取MP3中的歌曲信息
File file = new File(m_playlist.get(i));
map.put("song_name", file.getName());
map.put("info", "test");
map.put("img", R.drawable.panda);
m_list.add(map);
}
return m_list;
}
// 获取按钮
private void findView() {
// TODO Auto-generated method stub
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
last = (Button) findViewById(R.id.last);
next = (Button) findViewById(R.id.next);
pause = (Button) findViewById(R.id.pause);
}
// 播放音乐
void playMusic(String path) {
try {
m_musicplayer.reset();
m_musicplayer.setDataSource(path);
m_musicplayer.prepare();// 准备同步
m_musicplayer.start();
if (m_playlist.isEmpty() == true) {
Toast.makeText(this, "播放列表为空", 1000).show();
return;
}
m_musicplayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
nextMusic();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
// 上一首
void lastMusic() {
if (m_list_item == 0) {
m_list_item = m_playlist.size() - 1;
} else {
m_list_item--;
}
String path = m_playlist.get(m_list_item);
playMusic(path);
}
// 下一首
void nextMusic() {
if (m_list_item == (m_playlist.size() - 1)) {
m_list_item = 0;
} else {
m_list_item++;
}
String path = m_playlist.get(m_list_item);
playMusic(path);
}
// 选择列表项时,播放音乐
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
m_list_item = position;
String path = m_playlist.get(m_list_item);
playMusic(path);
}
// 当用户返回时结束音乐并释放音乐对象
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
m_musicplayer.stop();
m_musicplayer.release();
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}