How to access an audio in raw or assets folder by save it on sdcard or using app's installation address?

1k views Asked by At

I'm trying to make a music album app that has some song in it and can play them. I made the player but now I am stuck because my player , uses musics in an especial address on sd card and I need to either copy audios or use my installed app's asset (or row) folder that is on device (I don't know if there is this folder on phone's memory or not). If there is a better way please let me know.

here is my code:

public class SongsManager {
    // SDCard Path
    final String MEDIA_PATH = new String("/sdcard/piano/");
    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;
    }
2

There are 2 answers

0
Vaibhav Agarwal On BEST ANSWER

Asset Folder is readable folder you can't write or store files from SD card to assets folder. You can add some Mp3 files to your asset folder initially before preparing final build of your projects and can copy them to SD Card later.

Now you can access these files from SD Card or assets. You can store Mp3 songs from SD card to server only using Some RESTful POST web services.

Here is the link to copy files from assets to sd card

How to copy files from 'assets' folder to sdcard?

Hope it will help you

2
Sound Conception On

You can check if a folder exists with:

File myFile = new File(myPath);
if (myFile.exists()) {
    // The folder exists so use the files in this folder
} else {
    // The folder does not exist so use the assets from the apps raw folder
}

<Additional>

You do not want to show the address of the assets folder (listing the internal file structure of your app to the user would be bad), but yes you can list the files from your raw directory in your app, and allow your user to select from them. The following code fills the arrays soundNames[] and soundIds[] from the contents of your raw folder:

Field[] fields = R.raw.class.getFields();
String[] soundNames = new String[fields.length];
int[] soundIds = new int[fields.length];
for (int i = 0; i < fields.length; i++) {
    soundNames[i] = fields[i].getName();
    soundIds[i] = getResources().getIdentifier(soundNames[i], null, null);
}