无法从sdcard获取歌曲到listview,Android

时间:2012-02-20 11:46:51

标签: android

我困扰这个问题好几个小时而无法修复它。

我收到一条消息,比如等待调试器解决... 您认为代码中是否存在任何问题,或者调试器的conf应该存在问题?

这是我到目前为止的代码:

public class mp3 extends ListActivity{

 private static final String MEDIA_PATH = new String("/sdcard/");
 private List<String> songs = new ArrayList<String>();
 private MediaPlayer mp = new MediaPlayer();
 private int currentPosition = 0;

 @Override
 public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.mp3listview);
         updateSongList();

 }

 public void updateSongList() {
     File home = new File(MEDIA_PATH);
     if (home.listFiles(new Mp3Filter()).length > 0) {
             for (File file : home.listFiles(new Mp3Filter())) {
                     songs.add(file.getName());
             }

             ArrayAdapter<String> songList = new ArrayAdapter<String>(this,
                             R.layout.songitem, songs);
             setListAdapter(songList);
     }



 }
 @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));
     }
     }

 class Mp3Filter implements FilenameFilter {
     public boolean accept(File dir, String name) {
             return (name.endsWith(".mp3"));
     }
     }
        }

0 个答案:

没有答案