so I built my Music Play and it finally works. Thanks to some special people here. But at first I ran the app, it wouldn't display any music. So I looked at my code and it says final String MEDIA_PATH = ("/sdcard"); - I know, don't hardcode, but hey.. It works. So I went to my device, and moved some music to the root of my sd card. opened the app, and it had music listed "Woohoo".
But my question now is, how can I get my app to list all the music from my sd card? And not just from a single directory. :) Thanks.
Here is my code for finding music on my device -
package com.ascendapps.nexplay;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Environment;
public class SongsManager {
// SDCard Path
final String MEDIA_PATH = ("/sdcard");
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())!=null && 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;
}
/*
* The following class filters all files that have an extension of .mp3||.MP3
*/
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
}
Thanks in advanced, I would've done some research on this but my internet is super slow and I have cache of Stackoverflow downloaded so this website loads faster. Any help would be appreciated.