package com.android.music;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class SystemAudioPlayer extends Activity{
private String songName;
private List<Map<String,Object>> data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
songName = this.getResources().getString(R.string.song_name);
data = getFiles("/system/media");
this.setContentView(R.layout.system_audio_list);
ListView listView2 = (ListView)findViewById(R.id.setbuttonlist);
SimpleAdapter simpleAdapter = new SimpleAdapter(this,data,R.layout.list_items2,new String[]{"subImage","subTitle"},new int[]{R.id.subImage,R.id.subTitle});
listView2.setAdapter(simpleAdapter);
listView2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Map<String,Object> map = data.get(position);
Intent it = new Intent(Intent.ACTION_VIEW);
it.setDataAndType(Uri.parse("file://"+map.get("url").toString()), "audio/mp3");
it.setComponent(new ComponentName("com.android.music","com.android.music.MediaPlaybackActivity"));
startActivity(it);
}
});
}
public List<Map<String,Object>> getFiles(String path)
{
Log.v("LEIYONGHUI", path);
Log.d("SystemAudioPlayer", "@@@@@@@@@@@@@@@@@@@@@@ song_name : "+songName);
List<Map<String,Object>> fileList = new ArrayList<Map<String,Object>>();
File rootFile = new File(path);
File[] file_array = rootFile.listFiles();
Map<String,Object> map3 = null;
if(null != file_array && file_array.length > 0)
{
for(File file : file_array)
{
if(file.isFile())
{
String filePath = file.getPath();
String fileName = file.getName();
Log.v("LEIYONGHUI", filePath);
Log.v("LEIYONGHUI", fileName);
if((fileName.endsWith(".MP3") || fileName.endsWith(".mp3")) && !fileName.startsWith("bootaudio") && !fileName.startsWith("shutaudio"))
{
map3 = new HashMap<String,Object>();
map3.put("subImage", R.drawable.ic_mp_song_playback);
map3.put("subTitle", songName);
map3.put("url", filePath);
fileList.add(map3);
}
}
}
}
return fileList;
}
}
布局文件:system_audio_list.xml
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@+id/setbuttonlist" android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
list_items2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip">
<ImageView
android:id="@+id/subImage"
android:layout_height="40dip"
android:layout_width="40dip"
android:layout_gravity="center_vertical"
/>
<TextView
android:paddingLeft="10dip"
android:id="@+id/subTitle"
android:textSize="25dip"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>