0

im developing an audio player that reads all mp3 files from Sdcard and then plays it, right now i need to change its path, i want it to read mp3 files in assets folder, how can this be done ?

this is the code

public class SongsManager {
// SDCard Path
final String MEDIA_PATH = new String("/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()).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 (name.endsWith(".mp3") || name.endsWith(".MP3"));
    }
}

}

  • I think you should read this page : http://stackoverflow.com/questions/8495908/android-assetmanager – kmas Jun 01 '13 at 01:02
  • oh that's bad news, it says i cant get an absolute path to Assets, then how actually can i load the mp3 files inside my app folders, what about using raw, can i get an absolute path to this ? if not then what is the solution ? – user2415912 Jun 01 '13 at 01:13
  • You can access assets by fairly parallel mechanisms, just not literally as files in the filesystem. – Chris Stratton Jun 01 '13 at 02:02
  • what mechanisms can i use to access assets then ? can you please help me ? – user2415912 Jun 01 '13 at 13:40

2 Answers2

0

Write it in the data directory : Android - Creating a folder in the data/data/pkg/files directory

If I remember it is /data/data/your_package/ (you can see it in DDMS perspective of Eclipse)

Community
  • 1
  • 1
kmas
  • 6,401
  • 13
  • 40
  • 62
0

Try this code:

AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor());
player.prepare();
player.start();
Hemz
  • 223
  • 2
  • 10