ListActivity
試圖在記錄活動和新活動的SD卡打開保存記錄時,呈現出從externalsdcard該ListView控件啓動,但沒有打開SD卡內容的MP3在其中發揮文件關閉! 謝謝安卓:ListActivity無法打開SD卡MP3既不播放mp3
這裏是我的代碼
public class Droid extends ListActivity {
private static final String MEDIA_PATH = new String(Environment
.getExternalStorageDirectory().getPath());
private List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
private int currentPosition = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songlist);
updateSongList();
}
public void updateSongList() {
File home = new File(MEDIA_PATH);
if (home.listFiles(new Filter()).length > 0) {
for (File file : home.listFiles(new Filter())) {
songs.add(file.getName());
}
ArrayAdapter<String> songList = new ArrayAdapter<String>(this,
R.layout.song_item, songs);
setListAdapter(songList);
}
}
class Filter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".3gp"));
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
currentPosition = position;
playSong(MEDIA_PATH + songs.get(position));
}
private void playSong(String songPath) {
try {
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
// Setup listener so next song starts automatically
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
nextSong();
}
});
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
private void nextSong() {
if (++currentPosition >= songs.size()) {
// Last song, just reset currentPosition
currentPosition = 0;
} else {
// Play next song
playSong(MEDIA_PATH + songs.get(currentPosition));
}
}
}
songlist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/media"/>
</LinearLayout>
song_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
強制選項菜單
case R.id.droid:
Intent dr = new Intent(MainActivity.this, Droid.class);
startActivity(dr);
return true;
需要看到堆棧跟蹤 –
檢查ANS和給予迴應 –
播放 – Bona