0

我是 android 开发新手,我正在使用 mediaplayer 实现一个简单的应用程序。我对如何从 sdcard 访问所有 mp3 文件有点困惑。我正在使用以下代码从 sdcard 下的歌曲文件夹中检索歌曲。但是在歌曲文件夹中,我有更多的子文件夹,其中包含很多音频文件。我正在使用以下代码来检索歌曲。但它正在加载歌曲下的文件,而不是加载父文件夹(sdcard/songs)的其他子文件夹歌曲。你们能否帮助我了解如何从 SDCard 访问所有音频文件、播放列表、艺术家。

public class SongsManager {
// SDCard Path
final String MEDIA_PATH = new String("/sdcard/songs");
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

// Constructor
public SongsManager(){

}

/**
 * Function to read all mp3 files from sdcard
 * and store the details in ArrayList
 * */
public ArrayList<HashMap<String, String>> getPlayList(){
    File home = new File(MEDIA_PATH);


    if (home.listFiles(new FileExtensionFilter()).length > 0) {
        for (File file : home.listFiles(new FileExtensionFilter())) {
            HashMap<String, String> song = new HashMap<String, String>();
            song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
            song.put("songPath", file.getPath());

            // Adding each song to SongList
            songsList.add(song);
        }
    }
    // return songs list array
    return songsList;
}

/**
 * Class to filter files which are having .mp3 extension
 * */
class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return true;
    }
}

}

谢谢你,钱德拉。

4

3 回答 3

1

尝试这个。

String[] STAR = { "*" };
int totalSongs;
public void ListAllSongs() 
    {
        Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

        if (MusicUtils.isSdPresent()) {
            cursor = managedQuery(allsongsuri, STAR, selection, null, null);

            totalSongs = cursor.getCount();

            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    do {
                        String songname = cursor
                                .getString(cursor
                                        .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                        int song_id = cursor.getInt(cursor
                                .getColumnIndex(MediaStore.Audio.Media._ID));

                        String fullpath = cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DATA));
                        fullsongpath.add(fullpath);

                        String albumname = cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                        int album_id = cursor
                                .getInt(cursor
                                        .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

                        String artistname = cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.ARTIST));
                        int artist_id = cursor
                                .getInt(cursor
                                        .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));

                                                } while (cursor.moveToNext());
                }
                cursor.close();
            }
        }
    }
于 2012-08-22T07:29:29.423 回答
0

你正在做的方式似乎是可能的,但看起来它不是“机器人”。有关ContentProvidersMediaStore.Audio的结帐信息

在那之后,我相信 Chirag Raval 代码会变得更加清晰,您将能够进一步处理它。

于 2012-08-22T07:31:59.843 回答
0
//declare your variables
private ArrayList<Song> songList;

//create a clas where you define your data
public class Song {

private long id;
private String title;
private String artist;

public Song(long songID, String songTitle, String songArtist) {
    id=songID;
    title=songTitle;
    artist=songArtist;
}

public long getID(){
    return id;
}
public String getTitle(){
    return title;
}
public String getArtist(){
    return artist;
}
}


    public void getSongLists(){
    ContentResolver musicResolver = context.getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String sel = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
    String ext = MimeTypeMap.getSingleton().getMimeTypeFromExtension("mp3");
    String[] selExtARGS = new String[]{ext};
    Cursor musicCursor = musicResolver.query(musicUri, null, sel, selExtARGS, null);

    if(musicCursor!=null && musicCursor.moveToFirst()){
        //get columns
        int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
        int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
        int column_index = musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        //add songs to list
        do {
            long id = musicCursor.getLong(idColumn);
            String title = musicCursor.getString(titleColumn);
            String artist = musicCursor.getString(artistColumn);

            songList.add(new Song(id, title, artist));
        }
        while (musicCursor.moveToNext());

    }
   }
于 2016-07-04T21:11:33.597 回答