How load songs from special folder on sd card in my listview and play it?

巧了我就是萌 提交于 2021-02-10 22:21:48

问题


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?


回答1:


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");



回答2:


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.



来源:https://stackoverflow.com/questions/24054774/how-load-songs-from-special-folder-on-sd-card-in-my-listview-and-play-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!