Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display all music on SD card [closed]

I'm using code form this page:

http://z4android.blogspot.com/2011/06/displaying-list-of-music-files-stored.html

The code is working, but not soo good. When I'm trying to scroll down, the ListView keeps repeating the songs in the list.

I have been looking for some alternative code, but I have not found any.

Thanks for any help.

like image 730
Bjorn Avatar asked Jan 24 '12 21:01

Bjorn


1 Answers

Andreas answer is the right way to do this but that code does not get the absolute file path. This causes the mMediaPlayer.prepare(); to throw IOException: Prepare failed. status=0x1.

Here is the code to get the file path along with the file name:

private String[] mAudioPath;
private MediaPlayer mMediaPlayer;
private String[] mMusicList;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mMediaPlayer = new MediaPlayer();

    ListView mListView = (ListView) findViewById(R.id.listView1);

    mMusicList = getAudioList();

    ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, mMusicList);
    mListView.setAdapter(mAdapter);

    mListView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
    try {
        playSong(mAudioPath[arg2]);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
   });
}

private String[] getAudioList() {
    final Cursor mCursor = getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA }, null, null,
            "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");

    int count = mCursor.getCount();

    String[] songs = new String[count];
    String[] mAudioPath = new String[count];
    int i = 0;
    if (mCursor.moveToFirst()) {
        do {
            songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
            mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
            i++;
        } while (mCursor.moveToNext());
    }   

    mCursor.close();

    return songs;
}

Now that we have the absolute path, we don't need to get the path again. So:

 private void playSong(String path) throws IllegalArgumentException,
    IllegalStateException, IOException {

    Log.d("ringtone", "playSong :: " + path);

    mMediaPlayer.reset();
    mMediaPlayer.setDataSource(path);       
//mMediaPlayer.setLooping(true);
    mMediaPlayer.prepare();
    mMediaPlayer.start();
}

Make sure to use:

playSong(mAudioPath[arg2]);

instead of:

playSong(mMusicList[arg2]);

in the listview OnItemClickListener.

To get only the title of the track (Looks more elegant than the whole file name with extension) use:

`MediaStore.Audio.Media.TITLE`

instead of:

`MediaStore.Audio.Media.DISPLAY_NAME`
like image 85
Vikram Gupta Avatar answered Oct 14 '22 02:10

Vikram Gupta