package com.example.hhh.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.view.View;
import android.widget.AdapterView;
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 MainActivity extends ListActivity {
String SD_PATH = new String("/sdcard/");
List<String> songs = new ArrayList<String>();
MediaPlayer mp = new MediaPlayer();
ListView list;
File home = new File(SD_PATH);
ArrayAdapter<String> songList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
updateSongList();
list = (ListView) findViewById(R.id.listView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
mp.reset();
mp.setDataSource(SD_PATH + songs.get(position));
mp.prepare();
mp.start();
}
catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void updateSongList() {
File home = new File(SD_PATH);
if (home.listFiles(new Mp3Filter()).length > 0) {
for (File file : home.listFiles(new Mp3Filter())) {
songs.add(file.getName());
}
songList = (ArrayAdapter<String>) songs;
setListAdapter(songList);
}
}
}
答案 0 :(得分:-1)
使用以下代码
package com.materialexample;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class Mp3Filter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3"));
}
}
public class PlayerActivity extends ListActivity {
String SD_PATH = new String("/sdcard/");
List<String> songs = new ArrayList<String>();
MediaPlayer mp = new MediaPlayer();
ListView listViewssss;
File home = new File(SD_PATH);
ArrayAdapter<String> songList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*setContentView(R.layout.activity_player);*/
listViewssss = getListView();
updateSongList();
listViewssss.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
mp.reset();
mp.setDataSource(SD_PATH + songs.get(position));
mp.prepare();
mp.start();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void updateSongList() {
File home = new File(SD_PATH);
if (home.listFiles(new Mp3Filter()).length > 0) {
for (File file : home.listFiles(new Mp3Filter())) {
songs.add(file.getName());
}
songList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songs);
setListAdapter(songList);
}
}
}
不要错过更改您的活动名称。