Question

I want to load songs from my custom folder which I made on sd card and play it in via my App.

the path of this folder is on: /sdcard/Music/MyFolder

enter image description here

Now I have a code which loads all music which is located on the sc card. This code is working. But I dont want all music from sd card, I just want the songs in my folder MyFolder

Here's the code I have now:

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];
        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;
    }

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

    mMediaPlayer.reset();
    mMediaPlayer.setDataSource(path); 
    mMediaPlayer.prepare();
    mMediaPlayer.start();
}

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

Does anyone knows how to do?

Was it helpful?

Solution

use where condition in selection, like this.

final Cursor mCursor = getContentResolver()
    .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA }, MediaStore.Audio.Media.DATA
        + " LIKE '/storage/sdcard0/Music/MyFolder/%'", null, "LOWER("
        + MediaStore.Audio.Media.TITLE + ") ASC");

OTHER TIPS

Use the following function to get list of files:

public void listf(String directoryName, ArrayList<File> files) {
File directory = new File(directoryName);

// get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList) {
    if (file.isFile()) {
        files.add(file);
    } else if (file.isDirectory()) {
        listf(file.getAbsolutePath(), files);
    }
}
}

And on listview item click pass the path pf the file to mediaplayer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top