我是android开发的新手,我正在使用mediaplayer实现一个简单的应用程序。我对如何从sdcard访问所有mp3文件感到困惑。我正在使用以下代码从sdcard下的songs文件夹中检索歌曲。但是在歌曲文件夹中我有一些更多的子文件夹,它有很多音频文件。我正在使用以下代码来检索歌曲。但它正在加载歌曲下的文件但不加载父文件夹(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;
}
}
}
谢谢你, 钱德拉。
答案 0 :(得分: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();
}
}
}
答案 1 :(得分:0)
你正在做的事情似乎是可能的,但看起来它不是'机器人'。有关ContentProviders和MediaStore.Audio
的结帐信息在那之后,我相信Chirag Raval代码会变得更加清晰,你可以进一步研究它。
答案 2 :(得分: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());
}
}