Reputation: 160
So i found this tutorial http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/ on how to make a music player.
The thing is that it only gets mp3 files inside /sdcard/Music/ and I also want to get mp3 files that are inside folders inside Music folder.
So i've changed the function that returns the songs list to a recursive function but it doesn't work. could you spot my mistake in the code?
filesList is from type File[] and songList is ArrayList>. when this function is first called it gets path= "/sdcards/Music/"
public ArrayList<HashMap<String, String>> getPlayList(String path)
{
File home = new File(path);
filesList=home.listFiles();
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);
}
}
for(int i=0;i<filesList.length;i++)
{
if((filesList[i].isDirectory()))
getPlayList(filesList[i].getAbsolutePath());
}
return songsList;
}
Upvotes: 1
Views: 3455
Reputation: 688
this code work for me, try to add
String media_path = Environment.getExternalStorageDirectory().getPath();
String[] splitPath= media_path .split("/");
final String MEDIA_PATH = "/" + splitPath[1] + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";
and replace your java code to this
// Constructor
public SongsManager() {
}
/**
* Function to read all mp3 files and store the details in
* ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList() {
System.out.println(MEDIA_PATH);
if (MEDIA_PATH != null) {
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
// return songs list array
return songsList;
}
private void scanDirectory(File directory) {
if (directory != null) {
File[] listFiles = directory.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
}
private void addSongToList(File song) {
if (song.getName().endsWith(mp3Pattern)) {
HashMap<String, String> songMap = new HashMap<String, String>();
songMap.put("songTitle",
song.getName().substring(0, (song.getName().length() - 4)));
songMap.put("songPath", song.getPath());
// Adding each song to SongList
songsList.add(songMap);
}
}
but this code load slowly :(
Upvotes: 1
Reputation: 719561
I can see a few mistakes, but the biggest one is that when you recurse, you throw away the resulting map.
Other problems:
You recurse for every file that doesn't have one of the listed suffixes ... but you should really be recursing for directories only.
Your method calls listFiles(...)
up to three times per directory.
Your method could call listFiles(...)
on a File
that represents a file rather than a directory. That will return a null
and you will get an NPE if you don't test for it. (And since you recurse on files too ...)
A HashMap is probably a poor choice for representing a "song". Create a custom class.
Testing for some boolean expression == false
is lame. Use the !
operator!
Upvotes: 1